Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to create an object and use it in multiple classes  RSS feed

 
Ross Gerard
Ranch Hand
Posts: 53
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, my code is working fine but I know there are problems with it.
When I create st from the example I should only have to create it once but yet I have created it twice to get my code to work. Can anyone kindly help teach me the next step in my java practice?

Example:
student st = new student("Ross","Gerard",32);




















import java.awt.*;
import javax.swing.*;
public class CenterPanel extends JPanel
{









 
L Foster
Ranch Hand
Posts: 242
14
Android Angular Framework AngularJS Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Ross,
Welcome to Java, in case you have not been told that recently.

What you have there is a classic problem. As you have said, you should not have to create the object more than once. But, in order to avoid that, you must share the one you have constructed, between the different panels. If you do not, you not only have to keep repeating yourself (always to be avoided. See DRY), but you can also risk the states of the objects getting out of synch. That is especially important for GUI projects that will tend toward Model View Controller design pattern. Here, your student object is "the model". If you were to change that model object without sharing, from CenterPanel, the other panels would miss that change.

So, how do you share? In this case, you can make the Student object (BTW, the convention in Java, is to capitalize/mixed case, class names, and use camel case for objects) from ControlPanel, and then pass it into the constructors of the other two panels. They just need to have parameters declared in their constructors, and then to have "st" passed in, instead of just empty parentheses.

When I say this is a "classic problem", I mean that the way in which object states are obtained from wherever, can spawn whole architectural frameworks, spark lively discussions, and make people fortunes. But for now, know that you can pass the object reference in via the constructor, or you can make a "setter" in your code. Here's an example.



Have fun, and I hope this helps.
 
L Foster
Ranch Hand
Posts: 242
14
Android Angular Framework AngularJS Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to spell out the remarks I made about variable naming. Java uses a convention of mixed case, and no underscores, and capitalizing the first letter, when defining classes.

For example:


But, Java's convention uses mixed case with no underscores, but first letter lower case, when defining objects (or instances of classes).

Another example:


Note that the instance has "m" in lower case. That's called "camel case", because the humps are in the middle.

You may also define "constants" in Java, and their convention is very different. Here is an example of that:



Here, everything is in upper case, except that words are separated using underscores.

Luckily, modern IDEs use color, etc., to delineate all these types of things. But the differences can be subtle. It could almost be argued we don't need these conventions. But I think we still do.
 
Ross Gerard
Ranch Hand
Posts: 53
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, happy to be here! I always enjoy learning I appreciate your kindness in sharing.
 
Ross Gerard
Ranch Hand
Posts: 53
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I change the code here as suggested but now I have a different problem with a different object. CenterPanel centerP = new CenterPanel();
TopPanel topP = new TopPanel(); These two will give me problems if I try what you suggested.


 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should use st as an argument in creating your objects:
 
L Foster
Ranch Hand
Posts: 242
14
Android Angular Framework AngularJS Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Ross,
I can only guess this is because you did not pass in the "st" parameter. When you modify the constructor so it takes a parameter, and then do not pass in a parameter, it will fail. Using the setter is one way, and passing into the constructor is another way. If I led you to believe that you had to pass the value in both ways, then I was not clear enough.

Either way, if you change the constructor, you have to call it the new way.



But if you use the setter, it would be more like this:


I hope that clears it up. Since I did not see your whole code this time, I am only guessing this is what was wrong.
 
Ross Gerard
Ranch Hand
Posts: 53
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone for your help. Have a good day on the ranch and if you have any good coffee recipes let me know
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!