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.


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

Results 1 to 2 of 2

Thread: What does this line in the code mean?

  1. #1
    Junior Member
    Join Date
    Jul 2019
    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);
          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
    Eastern Florida
    Thanked 2,399 Times in 2,352 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.

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