-6

My problem is regarding the application of Static keyword for a class. As it is easy to apply static keyword for instance variables and methods but while coming to classes it is not working. finally please help me to solve the code

static class Box{
    static int width,depth,height;
    static void volume(int w,int d,int h){
        double vol=w*d*h;
        System.out.println(vol);
    }
}

class ClassStaticTest{
    public static void main(String[] args){
        //Box b=new Box();
        width=10;
        height=10;
        depth=10;
        Box.volume(10,10,10);
    }
}
sravani.s
  • 175
  • 1
  • 1
  • 10

3 Answers3

2

Top-level classes cannot be static, because the static keyword represents a relation between a class/member/method and the enclosing class.

As the top-level classes don't have an enclosing class, then the static keyword doesn't makes sense in this case.

Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
0

It makes no sense to make non-inner classes static.

static means: on the level of the containing class, and not instance of it.

In what you try to do: there is no containing class.

Stultuske
  • 9,296
  • 1
  • 25
  • 37
0

Only nested (inner) classes may be static. static has no meaning with regular classes

Alexey
  • 1,198
  • 1
  • 15
  • 35