|
-
Sep 27th, 2001, 11:45 AM
#1
Thread Starter
Dazed Member
Java Inconsistenties [resolved]
I noticed some things in Java that im not sure why they are allowed. Any comments?
1.) why does a constructor allow static members to be initialized?
2.) why is a non static member able to be initialized out side
of its class constructor?
Code:
// why does a constructor allow static members to be initialized?
// why is a non static member able to be initialized out side
// of its class constructor?
class Test2{
static int x;
static int w = 54;
int z = 56;
public Disassembler(){
x = 73; // ?
}
}
public class Test1{
public static void main(String[] args){
Disassembler d = new Disassembler();
System.out.println(Disassembler.x);
System.out.println(Disassembler.w);
System.out.println(d.z);
}
}
Last edited by Dilenger4; Oct 1st, 2001 at 03:19 PM.
-
Sep 27th, 2001, 01:15 PM
#2
Well ...
First, the code you have posted does not use a constructor, but I think that's a typo.
Second, using static variables inside a class means all objects derived from that class will share the same value for that variable. And if you want to make sure that the default/initial value of the static variable is other than zero or null, the constructor is the best place for it.
.
-
Sep 27th, 2001, 01:46 PM
#3
Thread Starter
Dazed Member
Sorry, yes that was a typo.
Code:
class Disassembler{
static int x;
static int w = 54;
int z = 56;
public Disassembler(){
x = 73; // ?
}
}
public class Test{
public static void main(String[] args){
Disassembler d = new Disassembler();
System.out.println(Disassembler.x);
System.out.println(Disassembler.w);
System.out.println(d.z);
}
}
Yes your right.
When a static variable or member (whichever you want to call it)
is declared within a class it's values are shared within all instances of the class. But declaring a static variable within a constructor wouldnt make sense because you are changing the value each time a new instance of the class is created and this defeats the concept of static variables.
-
Sep 27th, 2001, 02:23 PM
#4
Thread Starter
Dazed Member
Ok i see now. I knew that a static variables value is shared among all instances. So if a static members value is changed in the class constructor it's value is changed in all instances.
I coded this real quick and it seems to show this.
[/code]
class A{
static int x;
int y;
public A(int x, int y){
this.x = x;
this.y = y;
}
}
class B extends A{
public B(int x, int y){
super(x,y);
this.x = x;
this.y = y;
}
}
class C extends A{
public C(int x, int y){
super(x,y);
this.x = x;
this.y = y;
}
}
public class Test{
public static void main(String[] args){
C c = new C(10,20); // x = 10, y = 20
System.out.println(c.x);
System.out.println(c.y);
B b = new B(30,50);
System.out.println(b.x); // x = 30 , y = 50
System.out.println(b.y);
System.out.println(c.x); // since the static variable x is changed
System.out.println(c.y); // the new value should be reflected in the class C;
}
}
[/code]
Thanks
-
Sep 28th, 2001, 09:01 AM
#5
Well ...
Coming to think of it, initializing a static variable right when declaring it might be another good alternative. It's the same as initializing it in the constructor.
.
-
Sep 29th, 2001, 01:57 AM
#6
Well ...
I think it's actually better to initialize a static variable as soon as it is declared.
VB Code:
Class DisAssembler
{
static int I = 20;
DisAssembler()
{
}
}
Now, load as many instances of the class as you like. The initialization has already been done, and I save on a statement in the constructor which would otherwise be executed for each object, without having any particular advantage.
.
-
Sep 29th, 2001, 10:25 AM
#7
Thread Starter
Dazed Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|