Why can I use external non static classes and methods inside main, and can't with classes and methods defined inside the class?

StackOverflow https://stackoverflow.com/questions/18884706

  •  29-06-2022
  •  | 
  •  

Question

I naturally can have classes inside any class, so in my main method. I also can access to its methods and its attributes.

But this code isn't working. I know main to be static, so is it something like:

The main method runs and is it the one constructing any class, even the one who contains it.

Then, main should start, construct the class who contains it, and then any class or method inside.

package holamundo;


public class HolaMundo {


    public class AnotherClass {

        // Class body
    }

    public void method () {

        // Code
    }

    public static void main(String[] args) {

        AnotherClass a = new AnotherClass();
        method();
    }

So doing:

package holamundo;


public class HolaMundo {


    public static class AnotherClass {

        // Class body
    }

    public static void method () {

        // Code
    }

    public static void main(String[] args) {

        AnotherClass a = new AnotherClass();
        method();
    }

We could say main is running first, among the AnotherClass and method definitions?

Was it helpful?

Solution

I tried writing this up with my own words, but the SCJP 6 book does it way better, so here it goes:

A static nested class is simply a class that's a static member of the enclosing class

Perhaps to better understand, here's some code:

  public class HolaMundo {

    public static class AnotherClass {

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

       public static void hola(){
           System.out.println("Hola");
       }
    }

    public static void main(String[] args) {
        HolaMundo.AnotherClass.hola();

        HolaMundo.AnotherClass holaAnother = new HolaMundo.AnotherClass();
        holaAnother.hello();

    }
}

Note that the above code in the main() method will continue to work if you remove the occurrences of HolaMundo..

If you're confused, here's more from the book:

The class itself isn't really "static"; there's no such thing as a static class. The static modifier in this case says that the nested class is a static member of the outer class. That means it can be accessed, as with other static members, without having an instance of the outer class.

A static method (like main()), doesn't belong (and also cannot access) to any particular instance of any class. This means that a static method can create new instances of any class it has access to, even if it happens to be an instance of the class that declared said method in the first place.

Now, since in this case, main(), AnotherClass and method() are all static members of HolaMundo, then yes, this means that they all "run together". More specifically, they are loaded when the class is first loaded by the JVM (aka at Class Loading time).

OTHER TIPS

In Java, non-static nested classes have an implicit reference to an instance of the container class and so cannot be utilized in static contexts (like the main method) since there is no instance to reference.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top