Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
Can one array be used in a different class (in Java)?
I have a main class called "HelloWorld" and another class called "sArray." Can I refer to the string array, in the class sArray, through the main class like I would if it was an object class?
Object class Ex.
void printStates() {
System.out.println("cadence:"+cadence);
}
Main class to Object class Ex.
Bicycle bike1 = new Bicycle();
bike1.printStates();
Can I do this with an Array?
3 Answers
- SAMLv 41 decade agoFavorite Answer
If the array is a public variable in sArray class, then you can directly access that array from the Main class using an object of the sArray.
If that is private member, then you may need to add a accessor or a public getter method in the sArray class which you may call in the Main class using the object of the sArray class. For example,
public String[] getArray() {
return array;
}
In the main class..
sArray sa = new sArray();
String[] a = sa.getArray();
// code to process the array...
You may need to take a bit more care related to packages if the array is of protected or default type.
Hope this helps!!!
Regards
SAM
- husoskiLv 71 decade ago
Is that really a class called "sArray", or is it an object? There's a difference. A class is a type definition, and an object is an instance of some class type. By "object" class, I think you mean just "class". And when you say you have a "class sArray", I think you mean an array (or array of objects).
Get this distinction and use it when you talk and type. It will help you understand what you're doing, and help you to be understood when you ask a question.
I think you're trying to access an array that is a member of an object. If so, the answer is yes--provided that the member is visible. You make a member visible to all outside classes by declaring it "public", or invisible to all outside classes by declaring it "private". There are two other options, but for now you should declare all class members as either public or private.
So, if you have:
import java.util.Arrays;
public class HelloWorld
{
public static void main(String args[])
{
Bicycle bike1 = new Bicycle();
bike1.printStates();
System.out.println( Arrays.toString(bike1.someArray);
}
} // end HelloWorld
class Bicycle
{
private int cadence;
public String[] sArray;
public Bicycle()
{
cadence = 10;
sArray = new String[2];
sArray[0] = "string1";
sArray[2] = "string2";
}
public void printStates() // notice the public here!
{
System.out.println("cadence:"+cadence);
}
} // end Bicycle
That's just on-the-fly, so there may be typos. Notice that each member of Bicycle is either public or private. The default, if you don't specify it, is that the item (data member or method) is that it's visible if the caller is in the same package, or hidden if not. Since nearly all sample programs are written written without package statements, all of those classes are in the same unnamed default package and are mutually visible. It's generally considered to be a Bad Habit to depend on this.
Note also that the Bicycle class was not defined as "public". Java only allows one public class per source file, and the source file name must be <classname>.java. If you need to access Bicycle from classes other than HelloWorld, then you should make it public and put it into it's own source file named Bicycle.java.
- parzychLv 44 years ago
The ArrayList has to have the typedef. ArrayList13b61b51ab04eba4e6a0dcf517143b human beings = new ArrayList13b61b51ab04eba4e6a0dcf517143b(); for( RichPeople rp : human beings ) { rp.getAddress(); } or or you will ought to forged the object to learn the procedures you desire. probabilities of calling a ability on an undefined merchandise are obtainable. for spaghetti code, do it this way ArrayList myarraylist = new ArrayList(); myarraylist.upload(new human beings()); myarraylist.upload(new human beings()); myarraylist.upload(new human beings()); human beings human beings = (human beings) myarraylist.get(0); human beings.getaddress();