• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to get multiple classes to access the the same object with the same values in the fields.  RSS feed

 
Alex Lucard
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to get multiple classes to access the the same object.

I have four different classes each one in its on package
Class 1) TheMainClass
In Package com.abc

Class 2) TheRecordObject
In Package com.abc.theRecord

Class 3) StoreTheName
In Package com.abc.store

Class 4) ReadNameObject
In Package com.abc.outPut

This is the output I get
Store the value of Mike in TheRecordObject
The Full name is =null


This is the output I would like to get
Store the value of Mike in TheRecordObject
The Full name is =Mike

can someone show me in code what I need to change to get the output I am looking for.

Here is the java code for the four classes.



 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The most straightforward way would be for StoreTheName.fullName() to return the record and then pass it in to ReadNameObject.fullName();
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are the member variables static?
 
Alex Lucard
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault Why not
What is the relevance of your answer as to how to get this output I am looking for.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex Lucard wrote:Bear Bibeault Why not . . .
I think you need to learn what static means before you try any more programming.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex Lucard wrote:Bear Bibeault Why not
What is the relevance of your answer as to how to get this output I am looking for.

Because getting something to work the wrong way is still wrong.
 
Alex Lucard
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Alex Lucard wrote:Bear Bibeault Why not . . .
I think you need to learn what static means before you try any more programming.

Will changing them to something other than static get the output I am looking for.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you trying to learn how to program, or just trying to get some specific output written?

If the former, let us proceed. If the latter, let us know so we won't continue to waste time.
 
Aji Musa
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep, Alex I agree with Bear Bibeault. What is the output you are trying your hands on
 
Alex Lucard
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I find Bear Bibeault comment to be insulting. This is a site for people just getting start programming. The way you learn to program is by doing it. So to tell someone “Are you trying to learn how to program, or just trying to get some specific output written” is not helping.

I did post the output I did get and the out put I would like to get in the post.

This is the output I get
Store the value of Mike in TheRecordObject
The Full name is =null


This is the output I would like to get
Store the value of Mike in TheRecordObject
The Full name is =Mike
What I am trying to learn is who in java is to pass data/values from one class to multiple classes using a object or gettters and setters.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex Lucard wrote:I find Bear Bibeault comment to be insulting. This is a site for people just getting start programming. The way you learn to program is by doing it. So to tell someone “Are you trying to learn how to program, or just trying to get some specific output written” is not helping.

Because you are not approaching it with the right attitude. My original question was a perfectly legitimate and helpful one meant to get you to think about the way you are writing the code. Making the variables static rather than instance variables is clearly a mistake.

But instead of trying to answer it, you brushed it off as irrelevant which implied that you are not interested in learning how to do it right. Hence my follow up question. I've been writing code and teaching for almost 40 years. You can either choose to take advantage of that experience, or you can decide not to.

Your choice.

Alex Lucard wrote:
What I am trying to learn is who in java is to pass data/values from one class to multiple classes using a object or gettters and setters.


With that goal in mind, let's get back on track and I will repeat my first question: why did you make the variables static?
 
Alex Lucard
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not have a preference at this time on static or or no static.
If I remove the static from a class like ReadNameObject
Then I will need to change the main method.
Change the one line from
ReadNameObject.fullName();

to this next two lines.

ReadNameObject readIt = new ReadNameObject();
readIt.fullName();
but I still get them same output.
Store the value of Mike in TheRecordObject
The Full name is =null

I can do it but I still get the same out put. null.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex Lucard wrote:I do not have a preference at this time on static or or no static.

It's not a matter of preference. You stated a goal to learn how to pass data to objects. By using static you have removed objects from the equation, and so cannot proceed.

It's like saying, I need to learn how to adjust this carburator, but I didn't install one and have no preference for carburator or no carburator at this time.

If I remove the static from a class like ReadNameObject
Then I will need to change the main method.

Yes.

but I still get them same output.

Because there are still problems. But fixing the static problem is the first step.
 
Alex Lucard
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I did make the change and removed the key word static from both the StoreTheName and ReadNameObject.
Then changed the code in the TheMainClass to this.



I still get the same output. I think it is instantiating a new object TheRecordObject each time. What i think is missing is how to not make a new TheRecordObject object but to use the same TheRecordObject object for both method StoreTheName and ReadNameObject.
 
Alex Lucard
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To help clarify
What I think is going on is that in the class StoreTheName I am instantiating an new object from TheRecordObject and passing the value Mike into the the private String firstName by calling setFirstName.

Next from the ReadNameObject I am trying to access and print the value that was just stored in the firstName String from the StoreTheName class. Now here is were I think things are going wrong.
The ReadNameObject is not access the same object that was instantiate from the StoreTheName class.
Let me know if this is wrong but I think I need to fix the ReadNameObject to call the same object that was made in the StoreTheName class,
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. On line 9 you create a new object very time that the fullName() method is called. Then, at the end of the method, that object goes out of scope and is likely reclaimed by the garbage collector. Even if it is not, you longer have any reference to it.

So it's time to push the keyboard away and think about what you are trying to do. What is the relation between the StoreTheName and TheRecordObject objects?

you should also pick better class names. Naming classes after their operations is not conventional, nor do those names help understand what the objects are modeling. Objects are things, not actions, so their name should reflect that. Your classes should be named as nouns that represent what they model, not what they do.

In doing so, perhaps you may find that your model isn't particularly well suited to the task at hand.
 
Alex Lucard
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did get this to work I had to change just one line in the original class TheRecordObject.
I added the keyword static to the private String.

This is now my output.
run:
Store the value of Mike in TheRecordObject
The Full name is =Mike
BUILD SUCCESSFUL (total time: 0 seconds)
 
Steffe Wilson
Ranch Hand
Posts: 165
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex Lucard wrote:I did get this to work I had to change just one line in the original class TheRecordObject.
I added the keyword static to the private String.

This is now my output.
run:
Store the value of Mike in TheRecordObject
The Full name is =Mike
BUILD SUCCESSFUL (total time: 0 seconds)

I hate to disappoint you Alex but although that will give you the answer you are looking for in this simple test, it is not the right way to do it and it is not extensible - as soon as you create more record objects it will all fall apart. As Campbell pointed out earlier you need to understand what 'static' actually does if you are going to use it.

My advice at this stage would be to try and rewrite your program without any use of the word 'static' (except for the line 'public static void main(String [] args)' which must always contain the word static).

Hint: You were on the right lines with your previous post - you are creating two objects instead of one. Create the record object once, reference it thereafter. You need to figure how that can be achieved in your program and what needs to change to enable that. Take a look at examples online or in books if it helps, to see how objects are created and referenced.

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly, just getting the right output does not mean it is "working". Using static is wrong -- once again, you need to understand what that is actually doing so you can see why it creates the right output, but only by accident.
 
Alex Lucard
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Exactly, just getting the right output does not mean it is "working". Using static is wrong -- once again, you need to understand what that is actually doing so you can see why it creates the right output, but only by accident.

I am at a loss can you show me the same thing with out the static keyword.
Can you show me the way you are talking about this in code.
I learn better with code examples.
Can you make a java object with fields to hold strings and/or int values that other classes can pull that data out of.
Something like a TaxObj with a field to hold state tax value that one class can set and other classes can pull this value or string from.
It does not have to be a int for tax I was just using that as an example.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not a static to be found.

 
Alex Lucard
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Not a static to be found.



Thank you this will help me see how to use Static and non Static to get a object for use as a value holder.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Notice that the only static is for the main method's driver class, which isn't part of the model.

static is something that should be use very sparingly, and only under very specific circumstances, none of which apply to your case.
 
Alex Lucard
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Notice that the only static is for the main method's driver class, which isn't part of the model.

static is something that should be use very sparingly, and only under very specific circumstances, none of which apply to your case.


I am going to redo my code to fit the way you have it here.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!