Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: What does this line in the code mean?

  1. #1
    Junior Member
    Join Date
    Jul 2019
    Posts
    22
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default What does this line in the code mean?

    What does this mean?

    static {
    		cache = new Cache(); 
    	}


    The whole class is as:

    public class ServiceLocator {
       private static Cache cache;
     
       static {
          cache = new Cache();		
       }
     
       public static Service getService(String jndiName){
     
          Service service = cache.getService(jndiName);
     
          if(service != null){
             return service;
          }
     
          InitialContext context = new InitialContext();
          Service service1 = (Service)context.lookup(jndiName);
          cache.addService(service1);
          return service1;
       }
    }

    So what is the role of this code in the application? (Is it a nameless static method? What is it called?)

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,787
    Thanks
    53
    Thanked 2,429 Times in 2,380 Posts

    Default Re: What does this line in the code mean?

    That code creates an instance of a class and assigns the reference to that instance to a variable.

    It is in a static block.
    See the tutorial: https://docs.oracle.com/javase/tutor...O/initial.html
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2020
    Location
    chandigarh
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What does this line in the code mean?

    This code creates an instance of Cache and assigns the reference of Cache to that instance to a variable.
    This is in a static block.
    The specialty of the static block is:
    1. When objects created separately there is no need to call static block because it is associated with the class level.
    2. Static blocks are executed before the constructor and main function. Automatically managed by JVM that's why this block is nameless.
    3. It is used to initialize static data members. The static block gets executed when the class is loaded in the memory before the main().

  4. #4
    Junior Member
    Join Date
    Apr 2020
    Location
    Jacksonville, FL
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What does this line in the code mean?

    Thanks for explaining!

Similar Threads

  1. have code - need one line fixed please!
    By ash12 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 7th, 2012, 07:24 PM
  2. Replies: 10
    Last Post: September 16th, 2011, 07:49 PM
  3. enum, value of: Why is this code line structured the way it is?
    By SPACE MONKEY in forum Java Theory & Questions
    Replies: 5
    Last Post: March 28th, 2011, 09:15 AM
  4. Help with a line of my code.
    By Miical94 in forum Object Oriented Programming
    Replies: 1
    Last Post: March 7th, 2011, 11:00 AM
  5. Missing a line or two of code,b ut dont know what it is.
    By backdown in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 20th, 2011, 09:13 AM