-1

I'm a total noob at java, but while practicing it occurred to me that with the OOP design, every class inside the main class is going to have to be static right? In this code there is no way for me to call the class within main that isn't static (ShinyMetal). It seems like maybe I'm missing some point of why you would declare a class static or not. Thanks for your help!

public class Solution {

  public class ShinyMetal {

  }

  public static void main(String[] args) {
    ShinyMetal abcd = new ShinyMetal(); // error cannot be referenced from static context
    System.out.println(abcd.toString());
  }
}
Kevin Cruijssen
  • 9,153
  • 9
  • 61
  • 135
any mous
  • 11
  • 1
  • You haven't declared a class as static. – nicomp Sep 07 '18 at 15:25
  • `ShinyMetal` is an _inner_ class of `Solution` and since it's not static you'd need an instance of `Solution` to call the constructor on (read up on inner classes for more info). So there are at least 3 ways to solve that: 1. move `ShinyMetal` outside of `Solution`, either into its own file or make it non-public, 2. make `ShinyMetal` static, 3. create an instance via `new Solution().new ShinyMetal()`. – Thomas Sep 07 '18 at 15:26

3 Answers3

0

Does every class used in main class have to be static?

No.

The problem, in your case, is not that the ShinyMetal class is not static. The problem is that you're creating an instance of a non-static inner class without an instance of the outer class.

You need an instance of the outer class (you wouldn't need this if this code were in an instance context/method of the outer class):

ShinyMetal abcd = new Solution().new ShinyMetal();

If, however, your inner class has no link whatsoever to the outer class, then you can simply declare ShinyMetal as a static nested class, which will allow your current code to work.

ernest_k
  • 44,416
  • 5
  • 53
  • 99
0

Since your class is a non static nested class it contains a reference the it's parent class (in this case Solution).

Thus, in order to instantiate ShinyMetal you need an instance of the parent (i.e Solution).

In order to overcome this, you either need to declare ShinyMetal as static or instantiate Solution to then access it.

I suggest you have a look at the documentation here which explains all of this quite nicely:

https://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html

akortex
  • 5,067
  • 2
  • 25
  • 57
0

No, it doesn't have to be static. This is only because you call it from inside a static method (main in this case, which does need to be static). You can create an instance of your Solution class first, in which case ShinyMetal doesn't have to be static:

public class Solution {
  public class ShinyMetal {
  }

  public static void main(String[] args) {
    Solution solution = new Solution();
    ShinyMetal abcd = solution.new ShinyMetal(); // No longer gives an error
    System.out.println(abcd.toString());
  }
}

Try it online.

You could also call it from a non-static method, but you'd still need an instance of Solution to call that method then:

class Solution {
  class ShinyMetal {
  }

  public static void main(String[] args) {
    Solution solution = new Solution();
    solution.createAndPrintShinyMetal();
  }

  private void createAndPrintShinyMetal(){
    ShinyMetal abcd = new ShinyMetal();
    System.out.println(abcd.toString());
  }
}

Try it online.

Kevin Cruijssen
  • 9,153
  • 9
  • 61
  • 135