This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static Objects

 
Joseph Cho
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the best way to go about making a static class?

I know in C# you just declare the class static.. but In Java you cannot unless it's a subclass correct?

Also, if I declare a static reference to a class. I'm trying to wrap my head around this.. Does this mean whatever object the reference is pointed to, no matter what outside class makes an instance of the class holding the static object of another class.. it will always retain the same variables for that static class instance?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, with respect to your subject line, "Static Objects," there's no such thing as a static object in Java. Objects do not have the property of being static or non-static. Only member variables, methods, initializer blocks, and nested classes do.

Joseph Cho wrote:What is the best way to go about making a static class?


You simply declare the class to be static. Note that only nested classes can be static, since static means, "associated with the enclosing class, not with any individual instance." Since top-level classes don't have an enclosing class, it would be meaningless for them to be static.

I know in C# you just declare the class static.. but In Java you cannot unless it's a subclass correct?


Nested class, not subclass. The two are completely different.

Also, if I declare a static reference to a class. I'm trying to wrap my head around this.. Does this mean whatever object the reference is pointed to, no matter what outside class makes an instance of the class holding the static object of another class.. it will always retain the same variables for that static class instance?


No idea what you're saying here.
 
Joseph Cho
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No idea what you're saying here.


Lets say I have:




Can you explain what is happening in class C..
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joseph Cho wrote:
Can you explain what is happening in class C..


It's not clear exactly what you're confused about, but I'll try to guess. In the future though, please be more specific.

Each C instance has references to two different B instances. C's someMethod() sets the B class's static member variable "a"'s "one" member variable first to 2 then to 4. So there are two B objects, and the B class (not the B objects) has a static reference to a single A object, which in turn as a "one" variable.

You'd have the exact same results if you switched the b and b2 variables in someMethod():


since there's just a single "a" for the whole B class.

You'd also get the same results if you insted used the B class name, which is what you should do. It's poor practice to refer to static members through a reference variable.


Using the reference variables as you did can lead to confusion, as it can make it look like "a" is a non-static variable, and hence makes it look like there are two different "a" variables--one per object.
 
Joseph Cho
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay thank you!

That cleared up a lot of confusion.
 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In case it helps to clarify, note that the C# concept of a static class is not the same as in Java, so you can't translate directly.

Static members and methods work in the same way. But a static class in C# is a class that can only contain static members. Adding any instance variables or methods is a compiler error, as is trying to instantiate one. You can create classes in Java that can't be instantiated, but you can't enforce the rest of that constraint.

In Java you only have static classes when they are "nested". The static keyword isn't needed for that in C# because all nested classes behave like that - C# doesn't have inner classes like Java ones.
 
Campbell Ritchie
Sheriff
Posts: 48402
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Obviously that sort of C# construct is intended to make the compiler enforce the design of a utility class.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic