Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2007

Tip: Looking for answers? Try searching our database.

Java Programming Practice

Thread view: 
RFleming@NationalSteel.com - 14 Aug 2007 17:58 GMT
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
Joe Attardi - 14 Aug 2007 18:22 GMT
> 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.

Signature

Joe Attardi
jattardi@gmail.com

kaldrenon - 14 Aug 2007 19:27 GMT
On Aug 14, 12:58 pm, RFlem...@NationalSteel.com wrote:
> 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
Ben Phillips - 14 Aug 2007 19:37 GMT
> 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!
Lew - 14 Aug 2007 22:49 GMT
RFlem...@NationalSteel.com wrote:
>> Say I have a Screen superclass (I believe that is the right term)

<http://java.sun.com/docs/books/tutorial/index.html>

Signature

Lew

Joe Attardi - 14 Aug 2007 19:36 GMT
> 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!

Signature

Joe Attardi
jattardi@gmail.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.