wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Instance variable problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Instance variable problem" Watch "Instance variable problem" New topic
Author

Instance variable problem

Parul Sweetie
Greenhorn

Joined: Jan 17, 2013
Posts: 16

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

Joined: Apr 06, 2010
Posts: 4372
    
    8

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

Joined: May 03, 2004
Posts: 3
This is my solution. Make s2 as a static variable and modify the argument constructor of Go class.




- Alex Danesi - SCJP 1.4
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 741
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

Joined: Sep 03, 2012
Posts: 33
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Instance variable problem