0
  1. Is the static void main class the entrance to your program regardless of which class calls it... Or is it just the entrance method for that specific class...
  2. How on earth does this work:

    public class init{
        public static void main(String[] args){
            new init();
        }
    
        public init(){
            System.out.print("hi");
        }
    }
    

    I dont understand the way the program creates an instance of itself... why cant you just do this?

    public class init{
        public static void main(String[] args){
            start();
        }
    
        public static void start(){
            System.out.print("hi");
        }
    }
    
  3. What on earth does static do versus simple public.

Jimmyt1988
  • 20,466
  • 41
  • 133
  • 233
  • "*static void main **class***"? – Lion Oct 19 '12 at 23:27
  • 1
    You may want to read up these questions [Who calls the main function in java?](http://stackoverflow.com/q/3949642/1037210), [Why is the Java main method static?](http://stackoverflow.com/q/146576/1037210). – Lion Oct 19 '12 at 23:32

6 Answers6

1

static and public are different keywords. public is an access modifier. static just specifies a member to be class wide.

public static void main is so that the JVM knows at which point to start executing your program. It only chooses one entry point and ignores the others if any.

Either way works, but it all boils down to design. That is do you want your constructor to display a message, or delegate that responsibility to another method? And also that method does not have to be static either. You could have it like so:

new init().start() ;

This is why programming is awesome and such a PITA ;) as there are so many ways of getting the one thing done.

Lews Therin
  • 10,907
  • 4
  • 48
  • 72
1

1) main is how you "run" a Java class. A program can have multiple classes, each with their own main. When you run it you specify which class to run.

2) You could do that, but normally it's nicer to deal with instances of objects rather than a class. There are a variety of reasons to do this.

3) static means it's a class-level variable/method, as opposed to an instance variable/method. They can be called "on" the class itself (e.g., System.out) or on an instance (even a null reference) but that's considered poor form.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
1

Is the static void main class the entrance to your program regardless of which class calls it... Or is it just the entrance method for that specific class

It's the method to say the JVM where the program can start, very similar to main function in C/C++ programs. If you have more than 1 class with this method, then you should tell the JVM which class will be the program entrance.

What on earth does static do versus simple public

static keyword means the methods/variables belong to the class, simply private/protected/public/default means the methods/variables belong to the class instance (the objects).

How on earth does this work

You're creating an instance of the init class. There's nothing wrong doing that in Java code. In the class constructor, the program will print a Hi word in the console.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
0

Hmm, well you could do the second thing. To be sure, I just compiled it, it ran, and said:

hi>

Static doesn't really have to do with public, it's static vs. dynamic.. and public vs. private.

Please read up : What does the 'static' keyword do in a class?

By the way, shhh - you don't have to have the static keyword to start the program. You can just say:

public void main(String[] args)

and most of the time, you'd get by, although in your case, you don't!

When I remove the first static, It gives a:

java.lang.NullPointerException error . I need to figure out why.. so, stick to static void main til you know Java well enough!

Community
  • 1
  • 1
Caffeinated
  • 11,982
  • 40
  • 122
  • 216
0
  1. entrance to your program

  2. The class creates an instance of itself, because eventually, it may need to encapsulate a whole lot more data, instead of just doing a System.out.print

  3. static, in this case, defines a Class method -- ie, a method that's called by a class, not by an object instantiated from that class.

mrk
  • 4,999
  • 3
  • 27
  • 42
0

Java's entry point always is a specified main method. In your first example you create a new object of type init (Class name is init)
In the constructor of your init class you tell it to write hi. Your telling it to create a new instance with the new keyword. The constructor is the method that matches the class name.

The second example does not create any instance. You call the start method, which prints hi. You could also have written init.start(); because a static method always refers to a type(class), while non static methods always need a object reference to execute.
Here's a modified version of your first example to demonstrate this:

public class Init{
    public static void main(String[] args){
        Init i = new Init();
        i.goodBye();
    }

    public Init(){
        System.out.print("hi");
    }

    public void goodBye() {
        System.out.print("Good Bye");
    }
}
jlordo
  • 37,490
  • 6
  • 58
  • 83
  • Excellent thanks.. I think yours knocked my head into action... The problem is that day in and day out i do the code.. and i just wanted to explain just precisely how it works.. and was trying to think of a really simple example like a class being a dog and methods are it running and sniffing while properties are its hair colour and gender.. you know.. something really easy to grasp. – Jimmyt1988 Oct 19 '12 at 23:36
  • 1
    That's a good training example. You could also train inheritance when creating a pet class and deriving cat, dog and so on classes. – jlordo Oct 19 '12 at 23:39