Solved

easy question about creating objects, instantiation?

Posted on 2004-10-09
9
178 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
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 21

Expert Comment

by:MogalManic
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 24

Expert Comment

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

Expert Comment

by:sciuriware
Comment Utility
As I said.
;JOOP!
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
Comment Utility
>> 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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

728 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now