LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-14-2004, 06:46 AM   #1
Tru_Messiah
LQ Newbie
 
Registered: May 2004
Posts: 17

Rep: Reputation: 0
Java help (accessing array elemonts from another class or method)


Ok basically I have my main Method which is where I have initialized an array of objects. I want to access this array, the objects within and the actual methods within the object from within another class. How would I go about doing this???

I keep on coming up with errors.

Here is some of my code to help you...

public static void main(String[]args)

{


//Declare Variables
int menuChoice = 2;
boolean quit = false;

// Init carArry and set variables
Car [] carArray;
carArray = new Car [10];



//Possition In Array Manufact Model E.Size Reg Millage MPG Status
carArray[0] = new Car("Vauxhall", "Corsa", 1.3, "GP 03 CBT", 8123, 35, 'Y');
carArray[1] = new Car("Vauxhall", "Agila", 1.2, "AB 03 FTR", 10045, 38, 'S');
carArray[2] = new Car("Vauxhall", "Agila", 1.0, "FT 01 YUT", 21488, 40, 'N');
carArray[3] = new Car("Vauxhall", "Astra", 1.6, "RT 51 DRE", 15032, 32, 'S');
carArray[4] = new Car("Vauxhall", "Astra", 2.2, "DR 51 KLP", 19933, 25, 'Y');
carArray[5] = new Car("Ford", "Fiesta", 1.0, "GP 53 REW", 4987, 40, 'Y');
carArray[6] = new Car("Ford", "Fiesta", 1.3, "SR 01 BHG", 1997, 35, 'N');
carArray[7] = new Car("Ford", "Mondeo", 1.6, "WE 02 GTY", 17342, 33, 'N');
carArray[8] = new Car("Ford", "Mondeo", 2.0, "QW 52 EWQ", 12345, 25, 'Y');
carArray[9] = new Car("Ford", "Orion", 1.8, "SD 01 TYR", 24459, 28, 'N');
}

Also here is one method from my Car object..

// Read the millage attribute
public double getMillage()

{
return millage;
}


How would I go about accessing this method from another class???

Thanks alot...
 
Old 05-14-2004, 07:35 AM   #2
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
As it stands

carArray[7].getMillage(); // getMileage() maybe??

and any other classes will have to be created within the main() section so that the have the scope to access carArray. What errors you getting?
 
Old 05-14-2004, 07:44 AM   #3
Tru_Messiah
LQ Newbie
 
Registered: May 2004
Posts: 17

Original Poster
Rep: Reputation: 0
What do you mean by create within the Main() section???

Here is an updated look at the problem...

class hireCar

{
public static void start()

{
System.out.println(" This is the hire car option");
System.out.println ("");
System.out.println (carArray[3].getMillage());
}
}

This is a class that will be put into my program. At the moment I am using it to test accesing data from the array. I run my main method, and then half way through it runs this method from the hireCar class, but when I try and compile it keeps on coming up with the following error.

C:\Documents and Settings\Mr Ben\Desktop\Java 2\hireCar.java:9: cannot resolve symbol
symbol : variable carArray
location: class hireCar
System.out.println (carArray[3].getMillage());
 
Old 05-14-2004, 08:00 AM   #4
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
From what I can tell carArray is outside the scope of hireCar so you'd have to create a class that contains carArray that hireCar inherits from or make carArray global or do it something like this

Code:
class hireCar{

   private Car[] carArray;

   public hireCar(Car[] listOfCars){
         carArray=listOfCars;
       }

    public static void start()
    {
     System.out.println(" This is the hire car option");
     System.out.println ("");
     System.out.println (carArray[3].getMillage());
   }
}
Better still design a dynamic class that holds the all the cars and has methods like get car, add car etc. but you'll still have to take scope into consideration
 
Old 05-14-2004, 08:02 AM   #5
Tru_Messiah
LQ Newbie
 
Registered: May 2004
Posts: 17

Original Poster
Rep: Reputation: 0
How do I make carArray global???
 
Old 05-14-2004, 08:06 AM   #6
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
You don't really want to make it "global" as such, not good practice. Think of redesigning your classes. I can help later if you're still stuck.
 
Old 05-14-2004, 09:20 AM   #7
dave_starsky
Member
 
Registered: Oct 2003
Location: UK, Manchester
Distribution: Gentoo (2.6.10-r4) & Ubuntu
Posts: 145

Rep: Reputation: 16
you need to declare your array outside of your main method

you can make this private and use accessor and mutator methods to modify the array from outside of the class
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(linking?) problem compiling very simple array class btb Programming 3 08-02-2005 03:16 PM
Array declaration in class or main function??? redhatrosh Programming 4 03-15-2005 02:13 PM
Compile Java - .class, .java, .jar ? woranl Programming 2 11-09-2004 10:12 PM
PHP: how to use a method from a class in the same class ldp Programming 5 09-17-2004 09:52 AM
dynamic array in class help PTBmilo Programming 6 03-09-2003 02:35 AM


All times are GMT -5. The time now is 07:26 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration