polkadot
asked on
easy question about creating objects, instantiation?
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");
}
}
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");
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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!
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!
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");
}
}
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");
}
}
sudhakar_koundinya, just try and you will see both fail.
;JOOP!
;JOOP!
As I said.
;JOOP!
;JOOP!
>> 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.
I am looking at the concept level. not at compile level.
It is true they don't compile.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
make it static or create a new object:
in main:
One o = new One;
o.l.setText("Better");
;JOOP!