Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to work with classes in their own diferents class files

 
joe encalada
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to all, so far I was working with classes all of them in the same class file. But now I trying to develop complex programs so I have to use classes in their own class files.
Here is a short version of my problem. I need to modify a text label of class2 from class1 and both are instanced in GuiParent class
Here GuiParent in one class file:

Here Class1 in other class  file:

And finally Class2 in another class file:

So, how can I modify line 14 of  Class1 to solve my problem. Thanks.
 
Campbell Ritchie
Sheriff
Posts: 51439
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to design the application so each object has access to whichever other object it needs access to. Shoul‍d a panel have access to another panel? Shou‍ld the buttons be part of the pane class or variables in the method to set up the GUI? Shou‍ld a panel have another panel as a field?
 
joe encalada
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Campbell Ritchie, the answer for all your three questions is yes.
You need to design the application so each object has access to whichever other object it needs access to.

From GuiParent class I can get Class1 and Class2 but nnot the other direcction. I guess I need to get acces to variable class2 of GuiParent to solve my problem.
 
Henry Wong
author
Marshal
Pie
Posts: 22119
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

First of all, please don't call them "class files". The class files are the ones with the ".class" extension. In this topic, you are referring to source files.

joe encalada wrote:
From GuiParent class I can get Class1 and Class2 but nnot the other direcction. I guess I need to get acces to variable class2 of GuiParent to solve my problem.


As Campbell implied (I think), this is a bad design.... but... to answer your question. For two classes (or anything, actually) to share a variable, that variable has to be in scope for the components that need to share them. In your example, the class2 variable is a local variable, and hence, not in scope outside of the method.

You need to change the scope of the variable. Can it be an instance variable? Can you add a getter? etc.

Henry
 
joe encalada
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry thanks for your suggestions.
You need to change the scope of the variable. Can it be an instance variable? Can you add a getter? etc.

So, how the scope of this variables has to be?
I don't know about instance varibale, I'm gonna read aboit it.
 
joe encalada
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to all. I've modified my code and now I have two classes. GuiParentClass2 and ClassInterrelation(where I have the problem). Inside ClassInterrelation I have Class1 and Class2 as inner classes of ClassInterrelation so every thing works ok. But I can't use Class1 and Class2  as inner classes because they are to big in my program, so what can I do to solve this problem? Here my modified code:




So, because of the size in my program I'm trying to use ClassInterrelation like the code below where Class1 and Class2 are their own source file.

I hope you can help me.
 
Campbell Ritchie
Sheriff
Posts: 51439
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks as though you were guessing about your design. Have you been taught anything about design? Draw yourself a diagram with the objects on, and draw arrows showing which objects use which other objects. That includes the action listeners on your buttons (also include them if you implement the listeners as λs). You need two buttons and two panels and the listeners added to the buttons need access to the panels. At least I think that is what you want. Now make all the display components local variables in the method used to set up the GUI. You probably do not need to subclass anything.
 
joe encalada
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ritchie, I have an OuterClass like this:

My problem is InnerClass1 and Innerclass2 have grown too much.
So, questions are:
- How / what can I do to manage big innerClasses in order to have a nice reading program?
- Can I put those innerClases in their own source files and then call them to make it work as inner classes of OuterClass?

 
Paul Clapham
Sheriff
Posts: 21581
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are on the wrong track. When you do object-oriented design, the idea is to design how the objects behave and what the relationships between the objects are. So deciding in advance that you have to have inner classes is the wrong kind of decision to be making.

Right now it appears that you don't even know what the objects are. The evidence for that statement is that you've called your classes things like "Class1". The name for a class should describe the purpose, or function, of its objects. So start by deciding what your objects are and what they are supposed to do.
 
joe encalada
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul thanks you for your comments. Can you suggest me some information in orden to solve my questions?
 
Campbell Ritchie
Sheriff
Posts: 51439
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We already have suggested things. You need a frame object which the panels are added to, and two panel objects and two button objects (I think) one added to each panel. You will then want two listener objects, one for each button, and that is it.

There will be other objects which you might not notice. For example the text as the heading for the frame is an object (String) and the different colours are each represented by an object.
 
Paul Clapham
Sheriff
Posts: 21581
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
joe encalada wrote:Can you suggest me some information in orden to solve my questions?


I already did. So let's assume you have already decided what your objects are and what they are supposed to do. Now you have a question about those objects and how they are related, right? So post that question along with some proposed code for those classes. And of course the names of those classes will be names which tell us what the objects are supposed to do...
 
joe encalada
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I was traveling. I'll do a short example.
 
Paul Clapham
Sheriff
Posts: 21581
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent!

(By the way you're not the only person with this problem. It seems to me that a lot of students are taught basic procedural programming, with no understanding of objects, then are left to figure out objects and how to design for them on their own.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic