• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using an Object property in my App  RSS feed

 
Ranch Hand
Posts: 1785
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using the following code snippet how can I make the firstName property value accessible in my entire app?

 
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's hard to say without knowing the context of that snippet. Basically, I would expect a public "getter" method to return that value.

In fact, rather than accessing firstName directly, I would expect that field to be private in UserInfo, with its own getter method...
 
Steve Dyke
Ranch Hand
Posts: 1785
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have fixed my class as you suggested. But if I use say the code:



Any where besides the class where I have the code:



I get the message userInfo cannot be resolved.

This is where I am confused. In the programming language I am acustomed to, when I declare a variable value as public I can access that value globally.
So I figure if I have a class that has a firstName property. I create an object based on that class it should assign the value for the firstName property. Then any where in my app I should be able to get the value of userInfo.getFirstName().

Please let me know if I have it wrong and get me thinking correctly.
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let say you have two classes. StupidUser and BoringUser and both of them have the same field userFName. Now apart from your current class how would you distinguish between the the two variables.

userInfo.getFirstName() tells the compiler the exact class's userfname.
[ May 10, 2007: Message edited by: Anupam Sinha ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again, it's hard to tell without more context, but it sounds like you have some class containing the line...

UserInfo userInfo = new UserInfo(userfname);

This line declares a variable called "userInfo" that references a new instance of UserInfo. Because it's not static, this is an "instance variable," associated with an instance of whatever class it's defined in. So anywhere within that instance you can call userInfo.getFirstName().

But outside of that instance, the name "userInfo" has no meaning unless it's qualified with another variable that references the instance. For example, myInstance.userInfo.getFirstName().

Does that make sense?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by marc weber:
Again, it's hard to tell without more context...

It appears this is related to a bigger issue being discussed in another thread. You might want to clear up those fundamental design questions before things get too confused.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!