Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java program problem

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


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
  • Quote
  • Report post to moderator
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
Posts: 3
Java Java ME Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic