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 Class variable inheritance probs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Class variable inheritance probs" Watch "Class variable inheritance probs" New topic
Author

Class variable inheritance probs

Hugo Rune
Greenhorn

Joined: Nov 01, 2004
Posts: 4
Hi

I'm having a bit of a problem getting my head around inheriting class variables.

The situtaion is this I have a super class called MainObject which is shown below

public class MainObject
{
private static boolean debugMessageFlag = false;

public MainObject()
{

}


public void debugMessage(Object anObject)
{
if (this.getDebugMessageFlag())
{
System.out.println(anObject);
}
}

public static boolean getDebugMessageFlag()
{
return debugMessageFlag;
}
public static void setDebugMessageFlag(boolean aBoolean)
{
debugMessageFlag = aBoolean;
}
}



I then sub class this object and execute the code below

aSubClass.setDebugMessageflag(true);

this seems to set the MainObject classes class variable debugMesageFlag to true also.

What I thought would happen is that I could define the class variable and methods in MainObject and then sub class this object and that every sub class of the MainObject class would have its own class variable debugMessageFlag, but it currently seems that that every sub class refers to MainObjects debugMessageFlag variable.

Any suggestions where I'm going wrong?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi Hugo,

Welcome to JavaRanch!

You're going wrong at square one, I'm afraid, in your expectation that inheriting a class variable means that a subclass gets its own copy. It doesn't. A subclass inherits access to the parent class's copy, nothing more -- Java will not create a second copy of a class variable no matter what*.

* (unless there are multiple copies of the parent class loaded by separate ClassLoaders, but that's an advanced topic not really relevant here. I mention it just to keep those pesky language lawyers happy.)


[Jess in Action][AskingGoodQuestions]
Hugo Rune
Greenhorn

Joined: Nov 01, 2004
Posts: 4
Thanks for clearing that up for me Ernest

What I want to do is have the ability to switch some debug messages of mine on and off at will in any object that inherits from MainObject. I was hoping to be able to define this behaviour in MainObject and then just inherit it from there.

So is there another way of doing this using inheritence and a static variable in a good object orientented way or is it best if I just put a static var in each class I create with the accompaniying getter and setter class methods?
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
The code you posted has all objects of type MainObject and all objects of types derived from MainObject sharing one debug state. Is that what you want?


Mike Gershman
SCJP 1.4, SCWCD in process
Hugo Rune
Greenhorn

Joined: Nov 01, 2004
Posts: 4
Ideally what I would like to do is for each sub class of Main Object to have its own debug state which I would be able to set using class methods so I can easily change all objects of a particlar classes with one call.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

You could do something like this:



Now you can set the debug state of any class with

MainClass.setDebugState(ChildClass.class, true);

and any member method in any child class can say

Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
What about having a debugState variable in each subclass? I guess this would mean that each subclass needs setDebugState() and getDebugState() methods. I personally think this would be a more obvious solution than Ernest's, but it isn't necessarily any better.

Layne


Java API Documentation
The Java Tutorial
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by Layne Lund:
What about having a debugState variable in each subclass? I guess this would mean that each subclass needs setDebugState() and getDebugState() methods. I personally think this would be a more obvious solution than Ernest's, but it isn't necessarily any better.


It is more obvious, and also has the potential to be much better-performing. The only reason I proposed my version was because it was the closest you could get to the original poster's goal of doing everything in the base class. You could simply have static setDebugState() and getDebugState() methods in each class, and then all those methods would have to do would be to reference a flag -- but of course then you'd have to duplicate the code in each class.
Hugo Rune
Greenhorn

Joined: Nov 01, 2004
Posts: 4
Thanks for your answer Ernest

A very clever solution that I would never have thought of in a million years.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Class variable inheritance probs