Results 1 to 8 of 8

Thread: Easy question about Array + Variable Declaration

  1. #1

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935
    1. You can't resize an array; the simplest solution is to use a Vector (java.util.Vector, look at the Javadocs to figure out how to use it)
    2. Same problem; since you can't dynamically size the array you either have to recopy it to an array with length + 1 or use another class like Vector.
    3. Packages and classes are two completely separate things; I don't understand what you mean.

  2. #2
    DaoK
    Guest
    Yesterday you learnt me how to use package but how can I declare a variable to be able to use it EVERYWHERE in my project?

  3. #3

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935
    If by project you mean package then yeah:
    Code:
    package com.turtletips.sample;
    
    public class SomeSampleClass1
    {
        public static void main(String[] args)
        {
            SomeSampleClass2.someMethod();
        }
    }
    Code:
    package com.turtletips.sample;
    
    public class SomeSampleClass2
    {
        public static void main(String[] args)
        {
            SomeSampleClass1.someMethod();
        }
    }
    Both of those are legal. However:
    Code:
    package org.riverhill.rhhshn;
    
    public class SomeClassOutsideOfTurtleTips
    {
        public static void main(String[] args)
        {
            SomeSampleClass1.someMethod(); // ILLEGAL
            com.turtletips.sample.SomeSampleClass1.someMethod(); // legal
        }
    }
    Clearer?

  4. #4
    DaoK
    Guest
    I understand that but lets say that I have a variable that I use in package com.turtletips.sample; and I want to use it in package org.riverhill.rhhshn; how can I do ?

    You know in VB when I declare a variable in a MODULE i can use the variable in form1 .. form 2... in all form...how can I do that in Java.

  5. #5

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935
    Originally posted by DaoK
    I understand that but lets say that I have a variable that I use in package com.turtletips.sample; and I want to use it in package org.riverhill.rhhshn; how can I do ?
    Declare it as public, not private. Not a good OO habit apparently, though...

  6. #6
    DaoK
    Guest
    ok thx you

  7. #7
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Private members are only available to the class which declared them. Public memebers are accessible everywhere, bolth in there classes package and in other packages where it's class is visible.
    Protected memebers are accessible to classes of the same package and all subclasses of this class in any package that the class is visible. So you could declare you members protected then in the other package you could subclass that class with the protected memebers to get at them.

  8. #8
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Maybe your class could supply public getters and setters to get at and return values. If bolth classes are in diffrent packages then you should be able to import the package with the class you want to work with.
    ie...... import com.controls.Guage;
    Now the GetterSetter class can create an instance of the Guage class and call these methods to get or set the values.

    I havent done any cross directory programming yet so i haven't ran in to that problem. but im sure i will.
    Code:
    /*
     You would probably want to supply setters to changes the
     values, other than the constructor only providing the
     the initial values.
    */
      import import com.controls.Guage;
    
     public class GetterSetter{   
       public static void main(String[] args){
        Guage guage = new Guage(); 
         System.out.println("The current tempature is" + guage.getDewPoint());
         System.out.println("The current tempature is" + guage.getTemp());
       }
     }
     
     
      package com.controls.Guage;
    
     class Guage{   
      private double temp; 
      private double dewpoint; 
      
      public Guage(){
       this(73.5, 50.6);
      }
      
      public Guage(double temp, double dewpoint){
       this.dewpoint = dewpoint; 
       this.temp = temp;
      }
      
      public double getTemp(){
         
           return temp;
       }
      public double getDewPoint(){
          return dewpoint;
       }
     }

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