This site requires JavaScript, please enable it in your browser!
Greenfoot back
Zamoht
Zamoht wrote ...

2013/10/16

Subclasses and statics

1
2
bourne bourne

2013/10/16

#
danpost wrote...
public ClassA(int val)
I would make this second constructor protected. Unless specific reason not to. But under the impression that ClassA should only have value say, 10 - it should not let this constructor be used except by subclasses for defining a different value. Static really doesn't make a difference here, final gets the job done.
davmac davmac

2013/10/16

#
The thing is that I store/keep reference to the superclass. So I keep a reference to any subclass as lets say ClassA it could be ClassB, ClassC or even ClassD it doesn't matter. The thing is that when I call getValue() it should return a different value depending on the "real" class.
But the method is defined in ClassA and it is declared static. So the "real" class is ClassA. Even if you call it via a reference to some subclass, you are still calling the ClassA method. That is what "static" means in Java - that the method is a class method; it belongs to the class in which it's defined.
Okay I get it I just wanted to know if there was an other way to go around it.
For the example you've given, the only way is to make the method non-static. Then you also need to either make the value variable non-static also, or otherwise provide a separate implementation of the method for each class.
Zamoht Zamoht

2013/10/16

#
The reason why I want static is because I believe it will use less RAM if I have like 40 objects of a class and they all need a reference to a big array or something that takes a lot of memory. If I remove static and just use final I will have 40 unchangeable (if that's a word) arrays right?
bourne bourne

2013/10/16

#
Actually static still takes just as much RAM, and more in the case of single ints. A reference for each instance and the variable itself. If using arrays or something else then yes static is beneficial.
davmac davmac

2013/10/16

#
If you make the variable non-static, then each object has a copy of the variable. However you could still make the variable reference the same array, depending on how you initialise it. Eg:
class ClassA
{
    final static int[] init_array = {0,1,2,3};

    final int[] array;

    ClassA() {
        array = init_array;
    }
}

class ClassB extends ClassA
{
    final static int[] init_array = {4,5,6,7};

    final int[] array;

    ClassB() {
        array = init_array;
    }
}
With this example, each object of ClassA shares the array (they each have their own reference to the same array) and each object of ClassB shares a different array.
davmac davmac

2013/10/16

#
Actually static still takes just as much RAM, and more in the case of single ints. A reference for each instance and the variable itself. If using arrays or something else then yes static is beneficial.
This isn't correct. A static variable does not cause a reference to exist in each instance. Only one copy of the variable exists, and it is "part of" the class; it doesn't require a reference.
davmac davmac

2013/10/16

#
Sorry, my code above wasn't right. Here's the correct version:
class ClassA  
{  
    final static int[] init_array = {0,1,2,3};  

    protected final int[] array;  
    
    ClassA()
    {
        this(init_array);
    }

    ClassA(int [] init_array) {  
        array = init_array;  
    }  
}  

class ClassB extends ClassA  
{  
    final static int[] init_array = {4,5,6,7};  

    ClassB() {  
        super(init_array);
    }  
}
Zamoht Zamoht

2013/10/16

#
Okay thanks a lot davmac, danpost and bourne for your time, input and answers.
davmac davmac

2013/10/17

#
Just one further post. The above code puts a reference in every instance of ClassA. If you really, really need to save memory, you might want to avoid this. You could instead have:
    class ClassA    
    {    
        final static int[] init_array = {0,1,2,3};    

        public int[] getArray()
        {
            return init_array;
        }
    }    
      
    class ClassB extends ClassA    
    {    
        final static int[] init_array = {4,5,6,7};    
      
        public int[] getArray()
        {
            return init_array;
        }
    }  
You need to login to post a reply.
1
2