Results 1 to 7 of 7

Thread: Java Inconsistenties [resolved]

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    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.

  2. #2
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    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.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  3. #3

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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.

  4. #4

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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

  5. #5
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    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.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  6. #6
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    I think it's actually better to initialize a static variable as soon as it is declared.

    VB Code:
    1. Class DisAssembler
    2. {
    3.    static int I = 20;
    4.    DisAssembler()
    5.    {
    6.    }
    7. }

    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.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  7. #7

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Hummmm. Yes maybe.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width