0
package staticexample;

public class nonstatic {
    public static void main(String[] args) {
        non A = new non();
    }
    class non{
        public void greeting(){
            System.out.println("Hej");
        }
    }
}

package staticexample;

public class nonstatic {
    public static void main(String[] args) {
        non A = new non();
    }
}
class non{
    public void greeting(){
        System.out.println("Hej");
    }
}

I know you cannot access a non static method in a static reference but what exactly is the difference between the two and why is one of them working and the other is not?

I tried asking the same question from chatgpt but I got more confused, so I would really appreciate some unadorned answers. Thank you!

5 Answers5

1

It is actually pretty simple, let's establish 3 things first

1- static means the thing(variable, method) belongs to the class so you can access that (variable, method) without creating instances

2- this keyword is used to access current instance

3- main method is a static method, it belongs to the class, hence you can not use this inside it because there is no instance.

you are not explicitly typing this in your first example but class non is non-static part of class nonstatic so it's an instance so it's called using this which is not acceptable.

the second example, class non is not not part of class nonstatic so there is no this involved

Ahmed Nabil
  • 457
  • 1
  • 9
0

To answer your question below, the reason is that static methods do not depend on objects. Therefore there is no this;

In other words, you should think that the main method cannot see your internal class. If you want to new that inner class, you need to write it like this

package staticexample;

public class nonstatic {
    public static void main(String[] args) {
        non A = new nonstatic().new non();
    }
    class non{
        public void greeting(){
            System.out.println("Hej");
        }
    }
}

In other words, static methods are actually free from objects. Although your main method is in class nonstatic, when accessing this static method, you should not think it is in any object instantiated with nonstatic.

0

The non class of the first code is an inner class of the outer nonstatic class, whereas the non class of the second code exists as it is.

Thus, the first code

package staticexample;

public class nonstatic {
    public static void main(String[] args) {
        non A = new non();
    }
    class non{
        public void greeting(){
            System.out.println("Hej");
        }
    }
}

tries to create an instance of non, but this requires an instance of nonstatic, so it fails.

The proper way is to create an instance of nonstatic as follows.

package staticexample;

public class nonstatic {
    public static void main(String[] args) {
        non A = new nonstatic().new non();
    }
    class non{
        public void greeting(){
            System.out.println("Hej");
        }
    }
}

Refer to How to instantiate non static inner class within a static method? for more details.

Naetmul
  • 14,544
  • 8
  • 57
  • 81
0

According to Non-Static Nested Classes,

Just like instance variables and methods, inner classes are associated with an instance of the enclosing class

To instantiate an inner class, we must first instantiate its enclosing class.

If you want to create an instance of the inner class, you should do this:

class OuterClass {
    ...
    class InnerClass {
        ...
    }
}
OuterClass outerObject = new OuterClass()
OuterClass.InnerClass innerObject = outerObject.new InnerClass();

So either:

  1. make class non as static class, or
  2. create instance non A like non A = new nonstatic().new non();
static class non{
    public void greeting(){
        System.out.println("Hej");
    }
}
Hi computer
  • 946
  • 4
  • 8
  • 19
0

"... what exactly is the difference between the two and why is one of them working and the other is not? ..."

The reason the first example is not working is because new non() has no context.

Since non is an inner-class you'll need to access it from an instance of nonstatic.

For example,

nonstatic nonstatic = new nonstatic();
non A = nonstatic.new non();

Although, since non is nested, you could declare it as static; making it easier to reference.

public static void main(String[] args) {
    non A = new nonstatic.non();
}
static class non{
    public void greeting(){
        System.out.println("Hej");
    }
}

Additionally, the nonstatic identifier is redundant; so you could just leave the assignment the way it is.

public static void main(String[] args) {
    non A = new non();
}
static class non{
    public void greeting(){
        System.out.println("Hej");
    }
}

In your second example you simply have two classes, non-nested.
This doesn't have much use in Java; although I imagine it may help in certain situations.

Essentially, you can have more than one class within a file, as long as not more than one is declared as public.

So, for example, the following will cause an error.

public class nonstatic {
    public static void main(String[] args) {
        non A = new non();
    }
}
public class non{
    public void greeting(){
        System.out.println("Hej");
    }
}

Error

java: class non is public, should be declared in a file named non.java

Here is the Java tutorial on classes and nested classes.
Nested Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects).

And, here is a relevant discussion regarding your second example.
StackOverflow – Can a java file have more than one class?.

Reilas
  • 3,297
  • 2
  • 4
  • 17