wood burning stoves 2.0*
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
JavaRanch » Java Forums » Java » Java in General
Bookmark "java program problem" Watch "java program problem" New topic
Author

java program problem

akilaz sandakelum
Greenhorn

Joined: Aug 12, 2012
Posts: 3



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

Joined: Jan 03, 2004
Posts: 6109
    
    6

akilaz sandakelum wrote:
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"?


Static in Java means "associated with the enclosing class, rather than with individual instances." Since a top-level class has no enclosing class, it doesn't have the property of being static or non-static. It's meaningless. A nested class, however, can be 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?


Same thing as #1.

When you UseCodeTags(←click), your code has to go between the tags. I've fixed it for you this time.

Also, in the future, please UseAMeaningfulSubjectLine(←click) so that folks here will have an idea of what your problem is about. "Java program problem" is quite generic and could apply to pretty much every thread here.

And finally, Welcome to the Ranch!

akilaz sandakelum
Greenhorn

Joined: Aug 12, 2012
Posts: 3

I sincerely appreciate your effort to take time to help with my answer and I thank you a lot. And also I apologize for the inconvenience you faced in reading my question. As a novice I hadn't know how to ask a question proper way. I will correct my mistake in future & thanks for your advice.

But I still have a small problem. According to my knowledge , static classes, static variables and static methods are associated with the template. If we make the Box class "static" outside the DemoBox class , won't the Box class associated with the template of DemoBox class? I appreciate if you can explain a little far that scenario.

And I really appreciate if you can explain what you meant by "Since a top-level class has no enclosing class, it doesn't have the property of being static or non-static. It's meaningless."

And I came up with another question. Why can't we make DemoBox class into "static" (the class in which the main method is declared) ? Compiler gives an error.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

akilaz sandakelum wrote:According to my knowledge , static classes, static variables and static methods are associated with the template.


You mean the class? Yes, they are associated with the enclosing class in which they're defined.



Variable bar and method baz() are associated with the Foo class as a whole, not with individual instances of Foo.

If we make the Box class "static" outside the DemoBox class , won't the Box class associated with the template of DemoBox class? I appreciate if you can explain a little far that scenario.


No. Why would it be? If Box is declared outside of DemoBox, there's nothing to associate it with DemoBox. More to the point, the JLS, which defines the rules of Java, says you can't do that. It doesn't matter how much sense you might think it makes logically--if the JLS says no, then it's no.

And I came up with another question. Why can't we make DemoBox class into "static" (the class in which the main method is declared) ? Compiler gives an error.


Because then it would be a top-level static class, which is not allowed in Java. Just like what I described above, there's no enclosing class with which to associate it, so it doesn't make sense according to what "static" means in Java, and top-level static classes are not allowed by the JLS.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java program problem
 
Similar Threads
java program problem
access problems
Garbage collection doubt
Is Java language works as pass by reference mechanism in case of objects?
protected constructors