Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Instance variable problem

 
Parul Sweetie
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

2. class Game {
3. static String s = "-";
4. String s2 = "s2";
5. Game(String arg) { s += arg; }
6. }
7. public class Go extends Game {
8. Go() { super(s2); }
9. { s += "i "; }
10. public static void main(String[] args) {
11. new Go();
12. System.out.println(s);
13. }
14. static { s += "sb "; }
15. }


The problem is that we can,t use s2 in call to super as the instance has not been created yet...Please let me know how to compile this code.
 
Matthew Brown
Bartender
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it depends on what you're trying to do. It'll compile by replacing s2 on line 8 with "randomword", for instance.

It doesn't really make sense to ask how to make a made up piece of code that you'll never write in real like compile. There are loads of ways (like deleting the whole thing. It's more important that you know why it doesn't compile.
 
Alessandro Danesi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my solution. Make s2 as a static variable and modify the argument constructor of Go class.



 
Himai Minh
Ranch Hand
Posts: 1250
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Parul Sweetie wrote:
2. class Game {
3. static String s = "-";
4. String s2 = "s2";
5. Game(String arg) { s += arg; }
6. }
7. public class Go extends Game {
8. Go() { super(s2); }
9. { s += "i "; }
10. public static void main(String[] args) {
11. new Go();
12. System.out.println(s);
13. }
14. static { s += "sb "; }
15. }


The problem is that we can,t use s2 in call to super as the instance has not been created yet...Please let me know how to compile this code.


I put the originial code in NetBean platform IDE, it says "cannot reference to s2 before supertype contructor is called"
It means Game's contructor is never called in super (s2) and the string s2 is never assigned to any value.
Simply say, Game instance has not created yet and how can the String s2 exist?
 
Zhenyi Luo
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Parul Sweetie wrote:
2. class Game {
3. static String s = "-";
4. String s2 = "s2";
5. Game(String arg) { s += arg; }
6. }
7. public class Go extends Game {
8. Go() { super(s2); }
9. { s += "i "; }
10. public static void main(String[] args) {
11. new Go();
12. System.out.println(s);
13. }
14. static { s += "sb "; }
15. }


The problem is that we can,t use s2 in call to super as the instance has not been created yet...Please let me know how to compile this code.


Change String s2 = "s2"; to static String s2 = "s2"; would compile the code
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic