2

how to use getter setter in two different class

Class A{ int a = 10; GetterAndSetter gs = new GetterAndSetter(); gs.setValue(a); } Class GetterAndSetter { int a ; public void setValue(int a){ this.a = a; } public int getValue(){ return a; } } class B { int c; GetterAndSetter gs = new GetterAndSetter(); c = gs.getValue(); } 

While printing c it gives null. And tell me if it is valid or not.

3
  • If it's not valid,then how come you declare it prints null??? Commented Aug 20, 2014 at 12:12
  • Please provide at least code that compiles. What you have pasted does not even qualifies as pseudo code... Commented Aug 20, 2014 at 12:15
  • are you trying to practice writing getter/setter ? Commented Aug 20, 2014 at 12:19

6 Answers 6

10

Whenever you write this

GetterAndSetter gs = new GetterAndSetter(); 

what you're doing is to create a new instance of GetterAndSetter. Two instances that you create won't have any connection between them.

Inside class A, you create a new instance, and set its value. Inside class C, you create a new instance, and read its value. But because you've got two different instances, the value you're reading isn't connected with the value you're setting.

This is roughly like:

  1. I buy an envelope, and put some money inside it.
  2. Later on, I want to get the money back, so I buy a new envelope, and look for the money inside it.

You have to be looking in the same envelope that you put the money in, if you want to find it!

Sign up to request clarification or add additional context in comments.

4 Comments

Good analogy used---+1.
thanks its a nice example. please tell me how to use A's envelop in B to take money
You need to take the gs you created in A, and pass it to B. You could do this by passing it in a constructor to B, or by having a field of B of type GetterAndSetter, and then passing the instance to B in a separate method of B's.
5-star for envelope example ;)
4

In class A, your code creates a new instance of GetterAndSetter and sets a value to the property. In class B, however, your code creates again another new instance of GetterAndSetter , then gets the value.

The instances your code works with in classes A and B are not the same - hence you don't obtain the values set in A when trying to get it in B. The instance of GetterAndSetter created in B is not used anymore after the code in B exits.

To fix this, you need to pass a reference to the GetterAndSetter instance from class A to B. You can do this e.g. by passing it as a parameter to a method of B, or by creating a new instance of A in B and calling a method that provides an instance of GetterAndSetter.

An example of the first option (pass as parameter):

Class A{ ... GetterAndSetter createAndSet(); int a = 10; GetterAndSetter gs = new GetterAndSetter(); gs.setValue(a); return gs; } ... } class B { ... void getValueFromGetterAndSetter(GetterAndSetter gs) { int c; c = gs.getValue(); ... } ... } 

To connect the instances, we of course also need to have another piece of code (assuming instances of A and B exist already):

... b.getValueFromGetterAndSetter(a.createAndSet()); ... 

1 Comment

Obviously, I didn't post the complete code. You need to make sure thrat instances of both A and B are properly initalized and the instance of GetterAndSetter is correctly passed.
0

You have used different reference. You should use same reference so that only you can access the value.

Comments

0

you need to understand the basics of oops, you have created one instance inside class A and you are trying to access in Class B which is possible only if you pass the reference of that object from Class A to B. In that case you have to have the instance of GetterAndSetter which you have created in Class A in Class B, instead you have created another new instance which will create new reference in memory, and the class variable a will be null.

Comments

0

In your code, both class A and B create new objects for GetterAndSetter. Hence they are not shared between these classes. Thats why you are getting null.

I wounder how your code print null for C. I think it would be "0" instead.

Comments

-1

It is valid, here is what happens:

You create object gs in class A

Then you set the value of a of that object to 10

You then create another object gs in class B

Last but not least, you ask the object gs from class B what its value for a is.

Guess what, its NULL as you did not set its value anywhere so it wont return one.

1 Comment

What's invalid about the code? It's valid Java, aside from one minor typo ('class' vs 'Class'). The indentation is screwy, so it's hard to read.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.