Java Programming Practice

R

RFleming

Hello,

I have recently learned Java (under pressure) not in a bad way.. We
had a person leave the company and I was next best suited to take over
where he left off.....

Anyway, I have gotten pretty comfortable, but still have a long way to
go..

My question is one on passing objects from class to class.....

Say I have a Screen superclass (I believe that is the right term)
The screen creates one object from the class Model.
The class called Model with an array of 100 widgets objects from the
widgets class... Model.Widget(intIndex).blahblah
I have several screens, subclasses of the Screen class.
Each subclass gets Model passed into it when it is created
Example below
public class ScreenCircuitControl implements Screen {
public ScreenCircuitControl(Model model){
this.model = model;
}

private String getWidgetInfo(int intIndex){
return model.widget(intIndex).getinfo();
}

private Model model;
}
Example ends

Each other screen is set up similarly. I access the Widget class by
typing Model.Widget.......
Is this the most memory efficient/best way, or at least a decent way,
or is this bad?

It is my understanding that I am passing model by reference thus not
creating duplicate memory locations with memory allocated for 100
widgets in each screen....


Any comments would be greatly appreciated

Thanks

Ryan
 
J

Joe Attardi

I have recently learned Java (under pressure) not in a bad way..
Welcome to the wonderful world of Java programming!
Say I have a Screen superclass (I believe that is the right term)
The screen creates one object from the class Model.
This sounds more like composition than inheritance. Composition is when
an object contains one or more other objects.
The class called Model with an array of 100 widgets objects from the
widgets class... Model.Widget(intIndex). [snip]
I access the Widget class by
typing Model.Widget.......
Is this the most memory efficient/best way, or at least a decent way,
or is this bad?
The approach is sound. However, while Model.Widget(index) is completely
legal, I would offer two points of advice:
(1) Method names should start with a lowercase letter and follow
"camel case". For example, thisIsWhatIMean()
(2) Since your Widget method returns one of the Widgets, it is
recommended that you name it getWidget(int index).
It is my understanding that I am passing model by reference thus not
creating duplicate memory locations with memory allocated for 100
widgets in each screen....
You are correct. You are passing references to the Widget objects.
 
K

kaldrenon

Say I have a Screen superclass (I believe that is the right term)

You use the term superclass here. I don't know how much you've learned
yet about inheritance, classes, and interfaces, but I wanted to point
out that on the line below you have "ScreenCircuitControl implements
Screen". However, if Screen is a class, you need to use the keyword
"extends" to make ScreenCircuitControl a subclass of Screen. If Screen
is an interface, you did it right in the code, but that would make
superclass the wrong term to describe Screen.
public class ScreenCircuitControl implements Screen {
public ScreenCircuitControl(Model model){
this.model = model;
}

Does Screen have a constructor that takes a Model object? Because then
you could change this constructor to:

public ScreenCircuitControl(Model model){
super(model);
}

And everyone could chuckle at the pun. :p

-Andrew
 
J

Joe Attardi

Say I have a Screen superclass (I believe that is the right term)

Please ignore my earlier response about composition. I mis-read your
original message. Sorry about that!
 
B

Ben Phillips

kaldrenon said:
public ScreenCircuitControl(Model model){
super(model);
}

And everyone could chuckle at the pun. :p

ScreenCircuitControl foo = new
ScreenCircuitControl(DanielaPestova.getInstance());

eh -- damn, the screen melted!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top