File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inner Classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inner Classes" Watch "Inner Classes" New topic
Author

Inner Classes

Donald Nunn
Ranch Hand

Joined: Nov 11, 2000
Posts: 200
public class MyClass {
public static void main(String args[]){
State st = new State();
System.out.println(st.getValue());
State.Memento mem = st.memento();
st.alterValue();
System.out.println(st.getValue());
mem.restore();
System.out.println(st.getValue());
}

public static class State {
protected int val = 11;
// Method getValue
int getValue() {return val;}

// Method alterValue
void alterValue(){val = (val + 7) % 31;}

// Method memento
Memento memento(){return new Memento();}
class Memento {
int val;

// Method Memento
Memento() {this.val = State.this.val;}

// Method restore
void restore(){((State) this).val = this.val;}
}
}
}
In Khalid's book p.237 q.7.3 the answer is (e) The code will fail to compile, since the Memento method restore() tries an invalid access through the ((State) this).val expression.
I don't quite understand this. Could someone please clear this up for me.
Thanks,
Donald


<b>Donald Nunn</b><br />Sun Certified Programmer for the Java 2 Platform
james hoskins
Ranch Hand

Joined: Jun 28, 2001
Posts: 31
Hello Donald,
As far as I can see, this question is about remembering the special form of this syntax:
<enclosing class name>.this
which gives you a reference to the enclosing object (of enclosing class type).
The method in your code example uses:
((State) this).val
which seems to be casting reference this to class State. Remember, even if you make a legal cast to a superclass or interface the reference still refers to the same object - so you would still be trying to access Memento.val.
Kind regards,
James Hoskins
james hoskins
Ranch Hand

Joined: Jun 28, 2001
Posts: 31
Oops, my mistake. Special this syntax:
enclosing class name.this
regards,
james.
Donald Nunn
Ranch Hand

Joined: Nov 11, 2000
Posts: 200
Thanks, James. I'm still a little fussy.
The explanation in the book reads - The code will fail to compile since the Memento method restore() tries an invalid access through the ((State) this.val expression.
Below is the correct statement. I'm still not clear on what he means by invalid access. I apologize if it's not making sense to me yet.
void restore() { (State.this.val) = this.val; }
Thanks,
Donald
james hoskins
Ranch Hand

Joined: Jun 28, 2001
Posts: 31
Hello Donald,
If you attempt to compile the program you get an error message saying "invalid cast from MyClass.State.Memento to MyClass.State".
When you write " (State) this " you are trying to cast reference this to type State. Now, the reference this is a reference to the current object of type Memento and even though you find class Memento inside class State, class Memento is NOT a sub-class of class State. This means it is illegal to try and cast any reference to an object of class Memento to class State.
Memento mem = new Memento();
State state = (State) mem;
The above expression will never work unless you specify that Memento extends State.
Hope that is a little clearer!
Kind regards,
james.
Donald Nunn
Ranch Hand

Joined: Nov 11, 2000
Posts: 200
Thanks, James. That clears it up. Some of this stuff is tricky and I might not catch on for a while, however, explanations such as yours helps a lot.
Have a great day,
Donald
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inner Classes