Solved

easy question about creating objects, instantiation?

Posted on 2004-10-09
9
183 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

707 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