Click to See Complete Forum and Search --> : Easy question about Array + Variable Declaration
filburt1
Dec 31st, 2001, 10:06 AM
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.
DaoK
Dec 31st, 2001, 10:15 AM
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?
filburt1
Dec 31st, 2001, 10:18 AM
If by project you mean package then yeah:
package com.turtletips.sample;
public class SomeSampleClass1
{
public static void main(String[] args)
{
SomeSampleClass2.someMethod();
}
}
package com.turtletips.sample;
public class SomeSampleClass2
{
public static void main(String[] args)
{
SomeSampleClass1.someMethod();
}
}
Both of those are legal. However:
package org.riverhill.rhhshn;
public class SomeClassOutsideOfTurtleTips
{
public static void main(String[] args)
{
SomeSampleClass1.someMethod(); // ILLEGAL
com.turtletips.sample.SomeSampleClass1.someMethod(); // legal
}
}Clearer?
DaoK
Dec 31st, 2001, 11:31 AM
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.
filburt1
Dec 31st, 2001, 11:35 AM
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...
DaoK
Dec 31st, 2001, 12:23 PM
ok thx you
Dillinger4
Dec 31st, 2001, 03:47 PM
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.
Dillinger4
Dec 31st, 2001, 04:31 PM
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.
/*
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;
}
}
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.