[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

easy question about creating objects, instantiation?

Posted on 2004-10-09
9
Medium Priority
?
185 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 800 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
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!

 
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 1200 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

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…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

656 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