Results 1 to 6 of 6
  1. #1
    Icandoit is offline Member
    Join Date
    Oct 2013
    Posts
    12
    Rep Power
    0

    Default What does this do in java?

    I've seen this in some places, what is it and whats the proper use

    Java Code:
    public class Practice{
    	static {
    		System.out.println("Hello World");
    	}
    	public static void main(String... args) {
    		
    	}
    }
    Output : Hello World

    ??? (doesn't necessarily need to be static {}, it can just be {System.out.println("");})

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,456
    Rep Power
    25

    Default Re: What does this do in java?

    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Wim81 is offline Member
    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0

    Default Re: What does this do in java?

    Hi your code seems a bit strange

    it's more something like this form:

    Java Code:
    public class HelloWorld
    {
         public static void main(String[] args)
         {
            System.out.println("Hello World!");
          }
    }

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,618
    Rep Power
    5

    Default Re: What does this do in java?

    No the code is not strange. It simply demonstrates a static initializer block. What do you think this will print?

    Java Code:
    public class InitializerBlock {
      static {
         System.out.print("Hello, ");
      }
      public static void main(String [] args) {
         System.out.println("World!");
      }
    }
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    Wim81 is offline Member
    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0

    Default Re: What does this do in java?

    Aha ok my mistake! Didn't knew that static was also used that way.

    Regards
    Wim

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: What does this do in java?

    Quote Originally Posted by Wim81 View Post
    Aha ok my mistake! Didn't knew that static was also used that way.
    The only mistake is calling something you don't know about weird without googling it first ;)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •