Solved

easy question about creating objects, instantiation?

Posted on 2004-10-09
9
180 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
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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ArrayIndexOutOfBoundException 9 81
Arrays.asList  VS  ArrayList 4 56
login jsp example 24 49
tomcat startup error 5 63
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

770 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