Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

classes, help

 
Tim Tock
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
newbie here, hi all
Could someone show me how to read a variable from one class to another for example, i have a file called program1 and program 2, heres a snippet. In program2 I want to pull the value from program1 but the code doesnt seem to work, in program1 i have made the values public i.e. public int ... , ...;
program1:
public String toString() {
stringA += "my name"+myname+"last name" + lastname;
return stringA
}
program2:
System.out.println(program1.lastname);
 
Angel Dobbs-Sciortino
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using program1.lastname goes against proper encapsulation. You should use a get method. Also, you need to instantiate an instance of your Program1 object. Here's some example code:

Angel
[ January 15, 2004: Message edited by: Angel Dobbs-Sciortino ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic