Solved

easy question about creating objects, instantiation?

Posted on 2004-10-09
9
182 Views
Last Modified: 2010-03-31
ok what's the difference between how one and two are done, what are consequences of doing it different ways? which is more correct?

public class one{

  Label l = new Label("ok");
  public static void main(...
  {
       l.setText("still ok");
  }

}

public class two{

  Label l;
  public static void main(...
  {
       l = new Label("ok");
  }

}
0
Comment
Question by:polkadot
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 24

Expert Comment

by:sciuriware
ID: 12266539
One is wrong: the is no 'one' object to contain 'l';
make it static or create a new object:

in main:
   One o = new One;
   o.l.setText("Better");
;JOOP!
0
 
LVL 14

Accepted Solution

by:
sudhakar_koundinya earned 200 total points
ID: 12266547
both are OK

Label l=new Label("OK");

this creates the label at creation of instance


Label l=new Label("OK");

l.setText("Again OK");

this will be used if you want to change the text  dynamically
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12266572
Two is wrong for the same reason; else there is not so much difference:
   the original code of One expects that the label is created when the One object is created.
You can call that 'extended construction'.
But, both examples are wrong.

1) in cases like these, type the code in and try to compile.
2) the title of such questions should be more instructive for later readers.
You did not consider this easy, as you reward the question.
;JOOP!
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 21

Expert Comment

by:MogalManic
ID: 12266587
Neither class one or class two will compile!  The method main() is static and the variable 'l' is a member of the class Instance.  I'll fix your examples:
public class one{

  Label l = new Label("ok");
  public static void main(...
  {
       one o=new one();
       o.l.setText("still ok");
  }

}

public class two{

  Label l;
  public static void main(...
  {
       two t=new two();
       t.l = new Label("ok");
  }

}
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12266594
sudhakar_koundinya, just try and you will see both fail.
;JOOP!
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12266602
As I said.
;JOOP!
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12266605
>> sudhakar_koundinya, just try and you will see both fail.

I am looking at the concept level. not at compile level.

It is true they don't compile.

0
 
LVL 21

Assisted Solution

by:MogalManic
MogalManic earned 300 total points
ID: 12266618
After all of the problems are resoved and the class compiles and runs, the differences are basicly just personal preference and how you are using the classes.

The first class creates a default label(with text "ok"), then resets the text to "still ok".  It may be a few milliseconds slower because you are setting the label twice.

The second class does NOT have a default and relies on the caller to initialize and set the label.  It MIGHT be a few milliseconds faster, but it could create a NullPointerException if you forget to initialize the label.
0
 

Author Comment

by:polkadot
ID: 12266709
I was asking for concept, not syntax ... sorry didn't know how else to phrase the question.

and for the person that made this comment: "You did not consider this easy, as you reward the question." its not difficult, its just urget

Thanks for you help.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question