Bookmark Topic Watch Topic
  • New Topic

java program problem

 
akilaz sandakelum
Greenhorn
Posts: 3
Java Java ME Oracle
  • Mark post as helpful
  • send pies
  • Report post to moderator

class Box {

void myMethod() {
System.out.println("Box");
}
}


public class DemoBox {

public static void main(String args[]) {


Box c1 = new Box();
c1.myMethod();
System.out.println(c1);

Box c2 = new Box();
c2.myMethod();
System.out.println(c2);


}


}

I have two questions regarding with above code.

1) In the above code, if we set the DemoBox class to a static class (public static DemoBox) compiler gives an error. What is the reason for that? Why cant we set it to "static"?

2) And I have another question. If we set the Box class into "static " (static class Box) then again compiler shows an error. Why cant we set it to "static"? But i know that if we insert the Box() class
within the DemoBox() class we can set the Box() class into static & i know the answer for it. But why we cant set the Box() class as "static" outside the class in which the main method is declared?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Report post to moderator
Duplicate of http://www.coderanch.com/t/597873/java/java/java-program. Locking.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic