|
-
Dec 31st, 2001, 11:06 AM
#1
Thread Starter
Member
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.
-
Dec 31st, 2001, 11:15 AM
#2
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?
-
Dec 31st, 2001, 11:18 AM
#3
Thread Starter
Member
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?
-
Dec 31st, 2001, 12:31 PM
#4
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.
-
Dec 31st, 2001, 12:35 PM
#5
Thread Starter
Member
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...
-
Dec 31st, 2001, 01:23 PM
#6
-
Dec 31st, 2001, 04:47 PM
#7
Dazed Member
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.
-
Dec 31st, 2001, 05:31 PM
#8
Dazed Member
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;
}
}
Last edited by Dilenger4; Dec 31st, 2001 at 06:38 PM.
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
|