File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Static Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static Objects" Watch "Static Objects" New topic
Author

Static Objects

Joseph Cho
Ranch Hand

Joined: Feb 19, 2012
Posts: 42
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Feb 19, 2012
Posts: 42
No idea what you're saying here.


Lets say I have:




Can you explain what is happening in class C..
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Feb 19, 2012
Posts: 42
Okay thank you!

That cleared up a lot of confusion.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4490
    
    8

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

Joined: Oct 13, 2005
Posts: 40052
    
  28
Obviously that sort of C# construct is intended to make the compiler enforce the design of a utility class.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static Objects