• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to access a getter method from a different class?  RSS feed

 
Russy Bond
Greenhorn
Posts: 12
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have 2 classes. TestClassA has 1 getter and 1 setter. From my second class, TestClassB, I want to access the getter method of TestClassA. If I try to access it, I get null. How do I do it?
I do not want the methods to be declared as static. Can you please let me know how can the getter method value be printed in TestClassB (without marking anything as static)?


Now, in my classB i.e.

[Added code tags - see UseCodeTags for details]
Thanks,
Russy
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking at the above code...what would you expect it to print? You create a object of type TestA, but you never set it's name to BE anything.

You ARE accessing the method, but the method returns NULL because that is exactly what your object.name is set to at this point.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You already are accessing the getter method of TestA. But what you haven't done is initialise the name variable in TestA. That happens when you call setName (or myTestMethod) - but you haven't done that. So name still has its default value, which is null.
 
Russy Bond
Greenhorn
Posts: 12
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about not adding more details. Actually I am using a @BeforeClass annotation of TestNG which makes sure myTestMethod gets definitely invoked and the name variable also gets set. I am able to print the value in TestA class. My only problem is now how to print this name or make the getter name print the value in TestB class. I hope I am being clear this time.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know nothing about TestNG or using the @BeforeClass annotation.

What I know is this. Looking at these two lines of code:


Assuming that is what you have (an you have now called that assumption into doubt), you create a brand new TestA object on the first line. When you create a NEW instance of the object, it has its own String varible 'name', which has not been set to anything.

Try this...add a line between those two that does this:


and see what you get.
 
Russy Bond
Greenhorn
Posts: 12
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Fred. I know that would work but my requirement is such that I want to access a value that has already been set and I do not want to set it again the second time.
For e.g you could think of loading all the properties file and set the property values in ClassA. Now from a different ClassB, I want to get a property value (that has already been set in ClassA).
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Russy Bond wrote:Thanks Fred. I know that would work but my requirement is such that I want to access a value that has already been set and I do not want to set it again the second time.

That was Fred's point - you haven't set the value - at least not in this instance of the A class.

Russy Bond wrote:For e.g you could think of loading all the properties file and set the property values in ClassA. Now from a different ClassB, I want to get a property value (that has already been set in ClassA).

If, somewhere else in your code, you are creating an instance of A and initialising it with values from a properties file, then that is a different instance of A from the one you are creating in B.
If you want to use the original A instance in your B instance then you need to pass it to the B instance - maybe as an argument to the constructor or to a setter method.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Russy Bond wrote:Sorry about not adding more details. Actually I am using a @BeforeClass annotation of TestNG which makes sure myTestMethod gets definitely invoked and the name variable also gets set. I am able to print the value in TestA class. My only problem is now how to print this name or make the getter name print the value in TestB class. I hope I am being clear this time.

Like the others, I've never heard of either the annotation or TestNG, and I suspect that a proper answer requires knowledge of both.

One might ask: Why are you using this setup if you're a beginner? Java is complex enough without adding new layers...

Winston
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
let's try putting it this way...

You have your TestA class. You say "each and every instance of TestA will have a String called name that I can set.

Just like every single Dog can have a name.

You create a TestA...somewhere. You set its name to <something>. You prove this with your TestNG thing. This is like adopting a dog and naming it Fido.

Then, you create a BRAND NEW instance of TestA. You never set ITS name to anything. You have adopted a second dog, and never given him a name. So, when you ask Java to print out the second TestA's name variable, it is telling you that you never set it.

Did you mean for your name variable to be static, such that there is only one name variable, regardless of how many copies/instances of TestA you make?
 
Russy Bond
Greenhorn
Posts: 12
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Fred. That was a good example. I get the mistake now. I think if I do not want to set the name through the new instance, the only way out for me is to declare the getName method as static and call it by TestA.getName().
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Russy Bond wrote:Thank you Fred. That was a good example. I get the mistake now. I think if I do not want to set the name through the new instance, the only way out for me is to declare the getName method as static and call it by TestA.getName().

No. That is not the solution. If you do that then every dog you own will be called Fido. The name should be specific to each instance of the A class.
What you need to do is revisit your design.
Why don't you show us the whole of your test class and the code where the name is being set properly and then maybe someone will be able to see your problem.
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote: . . . maybe someone will be able to see your problem.
Maybe, Joanne? Maybe? Surely you meant, definitely

I know people will disagree with me but I like the idea mentioned in this how to write javadoc comments link that you must write a constructor for every class. Write a constructor which sets up every field, and require users to supply a String to be the name. You require that by writing (String name) as part of the constructor header.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I know people will disagree with me but I like the idea mentioned in this how to write javadoc comments link that you must write a constructor for every class.

No argument from me; but then I'm a proponent of explicitly putting in "extends Object" as well.

About the only exception I could understand (for a default constructor) is when you're creating an anonymous class.

Winston
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot write constructors for anonymous classes. As far as I can tell they use constructors from the named class being extended and would only require a default constructor if the class has no constructor or you instantiate an interface
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You cannot write constructors for anonymous classes. As far as I can tell they use constructors from the named class being extended and would only require a default constructor if the class has no constructor or you instantiate an interface

True. Duh.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!