my dog learned polymorphism*
The moose likes Beginning Java and the fly likes How to access a common class member (Component) by different objects of a class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to access a common class member (Component) by different objects of a class?" Watch "How to access a common class member (Component) by different objects of a class?" New topic
Author

How to access a common class member (Component) by different objects of a class?

sony vijay
Ranch Hand

Joined: Jun 27, 2010
Posts: 32
Hi,

I create two objects of Test Class and call initialize method of Test Class in InitializeTest.java. However, I am passing the same textbox component to the initialize method through both the objects.

Passing same textbox twice looks redundant to me. What is a better way to send the textbox component to Test Class such that it can be used commonly by both objects test1 and test2 ?

I simplified the code for better readability but the task I am trying to achieve is that I need to pass textbox from IntializeTest to Test only once and use it commonly by both the objects test1 and test2. In other words, I do not want to create textbox in Test Class.





Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

sony vijay wrote:
Passing same textbox twice looks redundant to me.


In almost all cases that is the right thing to do. It might be the case that it would make more sense for that TB to be a static member variable of the Test class, but that's unlikely. It would only make sense that way if that TB were a property of the class as a whole, which is not the same as being a per-instance property where all instances you're currently working with just happen to have the same value for that property. With a toy program like this, it's not really an issue, but in a real program, you'd almost always pass it as a parameter.
sony vijay
Ranch Hand

Joined: Jun 27, 2010
Posts: 32
Okay thanks. I just wanted to double check if my coding practice was fine
 
jQuery in Action, 2nd edition
 
subject: How to access a common class member (Component) by different objects of a class?
 
Similar Threads
Private variable access
I am getting clone() has protected access in java.lang.Object
Java Rule Round-Up
what happen if all constructor of a class be declared as private?
Object Reference Casting