Connecting Tech Pros Worldwide Forums | Help | Site Map

Refer to that Red planet Object...(outside the method it's in)

nomad's Avatar
Expert
 
Join Date: Mar 2007
Location: CA.
Posts: 570
#1: Jan 25 '08
Hello Everyone:
I back trying to learn Java again.
Here is what I'm trying to do.
1. How does the code inside the visitPennsylvania method refer to the variable with vale "red Planet".
I know to that you need to use. this.
But I can not figure out how to use it.
Here are my two class.
Expand|Select|Wrap|Line Numbers
  1.  import static java.lang.System.out; 
  2. public class EnglishSpeakingWorld {
  3. String mars = " red planet";
  4. void visitPennsylvania() {
  5. out.println("visitPA is running:");
  6. String mars = "Janine's home town";
  7. out.println(mars);
  8. out.println(this.mars);
  9.  
  10. }
  11.  
  12.  
Expand|Select|Wrap|Line Numbers
  1.  import static java.lang.System.out; 
  2. public class GetGoing {
  3. public static void main (String args[]) {
  4. out.println("main is running:");
  5. EnglishSpeakingWorld e = new EnglishSpeakingWorld();
  6. out.println(e.mars);
  7. e.visitPennsylvania();
  8.  
  9.  
Thanks
nomad
PS this is not a class project....

BigDaddyLH's Avatar
Moderator
 
Join Date: Dec 2007
Location: Kelowna, BC Canada
Posts: 1,212
#2: Jan 25 '08

re: Refer to that Red planet Object...(outside the method it's in)


Quote:

Originally Posted by nomad

Hello Everyone:
I back trying to learn Java again.
Here is what I'm trying to do.
1. How does the code inside the visitPennsylvania method refer to the variable with vale "red Planet".
I know to that you need to use. this.
But I can not figure out how to use it.
Here are my two class.

Expand|Select|Wrap|Line Numbers
  1.  import static java.lang.System.out; 
  2. public class EnglishSpeakingWorld {
  3. String mars = " red planet";
  4. void visitPennsylvania() {
  5. out.println("visitPA is running:");
  6. String mars = "Janine's home town";
  7. out.println(mars);
  8. out.println(this.mars);
  9.  
  10. }
  11.  
  12.  
Expand|Select|Wrap|Line Numbers
  1.  import static java.lang.System.out; 
  2. public class GetGoing {
  3. public static void main (String args[]) {
  4. out.println("main is running:");
  5. EnglishSpeakingWorld e = new EnglishSpeakingWorld();
  6. out.println(e.mars);
  7. e.visitPennsylvania();
  8.  
  9.  
Thanks
nomad
PS this is not a class project....

Works for me. Are you sure you compiled and ran your latest version?
nomad's Avatar
Expert
 
Join Date: Mar 2007
Location: CA.
Posts: 570
#3: Jan 25 '08

re: Refer to that Red planet Object...(outside the method it's in)


Quote:

Originally Posted by BigDaddyLH

Works for me. Are you sure you compiled and ran your latest version?

To much reading and I'm tried.
I was think from the [color=#000000]class[/color] GetGoing.
to println this.mars

nomad
Reply