Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Class variable inheritance probs

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic