-1

How to make the subclass int value to superside the final static int value in the main class? Example : My main class has

public class main {

final static int num = 2;
....
....
System.out.println(num);
}

My subclass is called in the main class.

Public class subclass {
....  -> How can I define the int num = 10 in the subclass without changing the main class code
...
}

The deal is not to touch the main class ie i cant change it as final static int num = 10; but i have to assign 10 to num.Do you know ?

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
jill
  • 9
  • 3
  • 5
    Please post real code if at all possible. Your formatting, spelling and capitalization errors, your lack of use of extends is distracting and confusing. As for your question, your num variable is marked final and its value cannot be changed. You can re-define a num variable in the child class of course. You need to tell more details of your actual problem. You need to show actual code though. – Hovercraft Full Of Eels May 12 '13 at 20:03
  • The whole point of final is to prevent exactly what you're suggesting. It is probably essential to the parent class that num=2 and that is why it was declared final. Why do you want to change num? – Richard Tingle May 12 '13 at 20:05
  • public class Battleship { final static int SHIPs = 2; static Scanner sc = new Scanner(System.in); static Grid game; public static int getMenuSelection() { System.out.println("\nRemaining battleshipss: " + (SHIPs - game.getShipsHit())); .... I have grid class (child) where i can re-define. I want to refine the value in child as super as its an assignment that I should not touch the main class and use the int in child class rather than the one declared in main class without changing the mail class – jill May 12 '13 at 20:09
  • 1
    Consider editing your answer to include the code – Richard Tingle May 12 '13 at 20:10
  • 1
    Important informations - like additional code - should be included in original question, not in comment. To include it in question use [[edit]] option. – Pshemo May 12 '13 at 20:11

4 Answers4

1

The simple answer is you can't really do this.

The long answer is you can change this value with reflection, however the javac compiler is free to inline this value so you will not be changing any place where this is used.

For example, after being compiled this is likely to be converted to

System.out.println(2);

so even if you change num, this will still print 2.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • So to print - 10 the only option is to declare or change the final staic int num to 10 in the main class? – jill May 12 '13 at 20:19
  • The only option involves recompiling the class, however you do it, so you know which values need to be updated. Note: if any other class uses this value, they need to be recompiled as well. – Peter Lawrey May 12 '13 at 20:26
1

You cannot change the value of a final variable.

You can define a variable with the same name in the sub-class, but since you are talking about static variables, parent/child class really has no meaning. Inheritance only applies to objects, not to class/static members.

public class SubClass extends main {
    final static int num = 10;
}

As I was getting at earlier, there is no inheritance in regard to static fields, and thus, no over-riding. The consequence of this is that num is only 10 if you reference the class directly.

System.out.println(main.num);
System.out.println(SubClass.num);

main parent = new main();
main child = new SubClass();
System.out.println(parent.num);
System.out.println(child.num);

outputs:

2
10
2
2
Brandon
  • 9,822
  • 3
  • 27
  • 37
  • You can change final values using reflection in almost every case, except this one. ;) – Peter Lawrey May 12 '13 at 20:13
  • 1
    Yea, I prefer to not recommend reflections as it just teaches people how to do things they shouldn't do. Plus, you're right, in this case, the compiler would likely inline references and thus changing it would have no value. – Brandon May 12 '13 at 20:21
0

Yes you can but before anything you must extend all superclass's behavior to it's subclass :

Public class subclass extends main{
private int num ;
//.....
// you can access superclass's static instance fields :
this.num =  main.num;

}

Note that since variable num is static you can call it via Class name. Also you can call it by super.num but the variable must not be in static way since static vairabes belongs to the class not to an object.

public class main {

final int num = 2;
//....
//....
System.out.println(num);
}
    Public class subclass extends main{
        private int num ;
        //.....
        // you can access superclass's static instance fields :
        this.num =  super.num;

        }
Azad
  • 5,047
  • 20
  • 38
0

You can just redeclare it in your subclass:

public class subclass extends main{

    static final int num = 10;
}

Ideally, static members should be called by using the className.propertyName, so it shouldn't matter if you redeclare the static property in your subclass.

Note:
Keeping the name of your class as main is not really good idea though.

Rahul Bobhate
  • 4,892
  • 3
  • 25
  • 48
  • This does not replace as the System.out.println(num); in the main class still takes the final static int num is the main class. Any other suggestion? – jill May 12 '13 at 20:18