File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class variable inheritance probs

 
Hugo Rune
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Hugo Rune
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1272
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Hugo Rune
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic