This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Java in General and the fly likes java program problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Java in General
Reply locked New topic

java program problem

akilaz sandakelum

Joined: Aug 12, 2012
Posts: 3

class Box {

void myMethod() {

public class DemoBox {

public static void main(String args[]) {

Box c1 = new Box();

Box c2 = new Box();



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

Joined: Jan 03, 2004
Posts: 6109

Duplicate of Locking.
I agree. Here's the link:
subject: java program problem
It's not a secret anymore!