John Smith
John Smith

Reputation: 25

Static Classes, Methods and main()

So, I'm a java noob and i just came across something that confused me. Getting to the point, I made a Foo Class and made an instance:

public class Main 
{
    public static void main(String[] args)
    {
        Foo foo = new Foo("Foo");
    }
}

class Foo
{
    public Foo(String A)
    {
        System.out.println(A);
    }
}

I noted that the Foo Class doesn't have to be static. Why? Whereas

if i do this

public class Main 
{
    public static void main(String[] args)
    {
        Foo foo = new Foo("Foo");
    }
    static class Foo
    {
        public Foo(String A)
        {
            System.out.println(A);
        }
    }
}

Then it HAS to be static. Why the difference? Static means it's instance independent hence every thing that is used in a static method also has to be instance independent(?) With Foo I was creating the instance in the static method so Foo didn't need to be static. But then what difference does having the class inside make? I thought i'd got the concept of static down. but apparently i lack a lot of concepts.

Upvotes: 2

Views: 126

Answers (5)

Yusuf K.
Yusuf K.

Reputation: 4250

Java is an Object Oriented programming language.

If you need a non-static declaration, field, method etc. from a class you need to create an instance from this class.

But to access static fields, methods you do not need to create an instance.

Now have a look at your example;

1.Main and Foo is different classes so you can create an instance from Foo everywhere.

public class Main {
    public static void main(String[] args) {
        Foo foo = new Foo("Foo");
    }
}

class Foo {
    public Foo(String A) {
        System.out.println(A);
    }
}

2.Foo is an inner class and Foo class is never accessible if an instance of Main not created. So If you create a Main instance you can create Foo too. But without creating a Main instance, only accessing Foo is defining Foo class as static

public class Main {
    public static void main(String[] args) {
        Foo foo = new Foo("Foo");
    }

    static class Foo {
        public Foo(String A) {
            System.out.println(A);
        }
    }
}

Upvotes: 0

Gourab Sarkar
Gourab Sarkar

Reputation: 80

In java, we can’t make Top level class static. Only nested classes can be static. So,For 1st scenario you will not be able to make the Foo class as static. for the 2nd Scenario Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class.

Upvotes: 0

Eran
Eran

Reputation: 393781

In the second code snippet, Foo is nested inside Main. Nested classes can be either inner classes or static. If they are inner classes (non static), each instance of the inner class must be associated with an instance of the enclosing class.

Therefore, if you remove the static keyword from Foo class in the second snippet, you'll have to instantiate it with :

Foo foo = new Main().new Foo("Foo");

When the nested class is static, it doesn't require an instance of the enclosing class, and you can instantiate it with

Foo foo = new Foo("Foo");

On the other hand, in your first snippet, Foo is not nested inside Main, so you don't need an instance of Main in order to instantiate it. And it cannot be static, since top level classes can't be static.

I thought i'd got the concept of static down. but apparently i lack a lot of concepts.

When applied to nested classes, the static keyword has a different meaning than it has when applied to methods and variables. You should read about nested classes.

Upvotes: 1

Stephen Rosenthal
Stephen Rosenthal

Reputation: 773

In the top example, you have two independent classes in the same file. main can reference Foo because it's just another class.

In the bottom example, you have a static nested class. It's also able to be instantiated independently. If you got rid of the static keyword on Foo you'd have an inner class, tied to a particular instance of Main, but when you are running the static main method there is no particular instance of Main to reference.

This reference might help: https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201429

This is covered by JLS-8.1.3. Inner Classes and Enclosing Instances which says (in part)

An inner class is a nested class that is not explicitly or implicitly declared static.

An inner class may be a non-static member class (§8.5), a local class (§14.3), or an anonymous class (§15.9.5). A member class of an interface is implicitly static (§9.5) so is never considered to be an inner class.

A static class is thus not an inner class, and an inner class requires an instance of the enclosing class; like

public class Main {
    public static void main(String[] args)
    {
        Foo foo = new Main().new Foo("Foo");
    }
    class Foo
    {
        public Foo(String A)
        {
            System.out.println(A);
        }
    }
}

Upvotes: 2

Related Questions