0

I have never really understood why code such as that given below is valid and would really appreciate it if someone helped me out.

public class A{

    int x;
    int y; 

    void hello() {
        System.out.println("Hello World");
    }

    public static void main(String[] args) {
        A my_instance = new A();

        my_instance.hello();
    }
}

OUTPUT:

Hello World

Question: Why are we allowed to create an instance of A inside one of its own static methods?

I understand that static methods belong to the class and not any particular instance, but does this mean that under the hood the compiler first analyzes everything that's not static and makes that code available to static methods?

  • 3
    "Why are we allowed to create an instance of A inside one of its own static methods?" - why *wouldn't* you be allowed to do that? You can create an instance of A pretty much anywhere. There's nothing special about A's static methods. – user2357112 Apr 05 '17 at 04:37
  • 1
    Possible duplicate of [Why make a class create an instance of itself?](http://stackoverflow.com/questions/22263772/why-make-a-class-create-an-instance-of-itself) – Zubair Nabi Apr 05 '17 at 04:40
  • 1
    It just seems wrong to me that I can create an instance of something that I'm still in the process of building. I don't understand enough about what is really going and why this is allowed – Samantha Smith Apr 05 '17 at 04:40
  • Keep in mind, the compiler has finished it's task before the program is run. What is the specific problem you fear? – Henry Apr 05 '17 at 04:41
  • Because if you couldn't you would have one very long `main`. – Guy Apr 05 '17 at 04:45
  • I don't really have a problem I fear at the moment, I've just always been curious why code like this works. For example, does this mean that the compiler separates out static from non-static and makes static code available to non-static code first? – Samantha Smith Apr 05 '17 at 04:46
  • Also, this is not a duplicate of the question posted. That question answers when this technique is needed, not why it works... – Samantha Smith Apr 05 '17 at 04:47
  • @ZubairNabi Can you please explain what part of my question is answered in the linked to question, since you saying it was a duplicate has lead to downvotes and decreased the chance my problem being seen by the right person who can answer it? – Samantha Smith Apr 05 '17 at 05:00

4 Answers4

2

See if these points help you.

1. Execution of a Java program starts with a special method public static void main(String[] args). Since Java is an object oriented language, everything has to be inside class, and this special method main() is no exception. That's why it is inside a class, namely A here.

2.

It just seems wrong to me that I can create an instance of something that I'm still in the process of building

This idea of yours is wrong. When the program is running, everything is built already (surely you know the compilation comes before running), so the notion in the process of building doesn't make any sense when the program is already up and running. And if you associate your statement in the process of building to the building of instance of A, then again that would be wrong, since the main() method is static, so doesn't require an instance of A to exist in order for it to be invoked. In fact, main() is invoked before JVM instantiates any other class in your application.

3. The main(String[] args) being the starting point of execution, it must be allowed to instantiate some class. Now, your class A is a class just like any other, hence main() can instantiate that too.

Some more reading here:

  1. Why is the Java main method static?
  2. Why make a class create an instance of itself?
Community
  • 1
  • 1
Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90
0

It works because you can create an object in any context. It works because if it didn't work you could never create an object. Static members are made available on class initialization, not "available to non-static code first" but to anything, static or not, after class initialization completes. main doesn't even run until after class initialization. At that point all the power of the class is available to static and non-static code alike, and that includes the power to create objects. It works because the JLS says that's how it works.

Lew Bloch
  • 3,364
  • 1
  • 16
  • 10
0

JVM takes care of the execution of static members , in this case , static method in such a way JVM designed .Because , we need to create a object from a static members

VNT
  • 934
  • 2
  • 8
  • 19
0

It seems contrary to the programmer because it seems as though an instance of the class is "willing" itself into existence, if you are reading the code as a procedural script. But this isn't a line after line procedure that you are looking at. This is code that is being compiled, then run.

Also yes, in the real world cups don't become cups, and clouds don't become clouds by internal calls that "will themselves" into existence, at least not as far as we can tell. But this is programming, and you are allowed to create these kinds of situations. Kind of like looking at the place where the snake bites it's tail, right? You'll get used to it.

Sometimes the answer is technical, and sometimes in programming you need to respect the abstraction process. Stand too close to a Monet and it looks like a bunch of dots.