File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Using method on a program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using method on a program" Watch "Using method on a program" New topic
Author

Using method on a program

Alfonso Saballett
Greenhorn

Joined: Feb 25, 2009
Posts: 9
Hi All

We are suppose to create methods and then develop a program that demonstrates each method.

Here is the Class that I developed, after I show you the Class I will post the program.



OK now for the program I developed to use the methods:



So when I do this I get the error: variable driversName might not have been initialized and same for the driversAge. Can someone help please.
anu kuntala
Greenhorn

Joined: Sep 25, 2008
Posts: 17
You have declared drivresName and driversAge as Local variables. You cannot use localvariables without initializing them. Declare the variables outside the main method.Try with this code.
Alfonso Saballett
Greenhorn

Joined: Feb 25, 2009
Posts: 9
I tried it and I got the following error:

"non-static variable driversName cannot be referenced from a static context" and the same with driversAge.

any ideas.

Thanks
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

OR, initialize them at the time of declaring !


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Alfonso Saballett wrote:

"non-static variable driversName cannot be referenced from a static context" and the same with driversAge.



That's means, static method can see static varibale and methods only, so to access "driverName" and "driverAge", declare them static !!
Harshit Rastogi
Ranch Hand

Joined: Apr 15, 2008
Posts: 131
Alfonso Saballett wrote:
So when I do this I get the error: variable driversName might not have been initialized and same for the driversAge. Can someone help please.


Then initialize them inside main.


the best way is to pass the drivername and age in the constructor.. So you need to create a parameterized constructor


<a href="http://technologiquepanorama.wordpress.com" target="_blank" rel="nofollow">My Techie Blog</a><br /><a href="http://www.java-questions.com" target="_blank" rel="nofollow">Java Questions</a>
Alfonso Saballett
Greenhorn

Joined: Feb 25, 2009
Posts: 9
Thanks guys.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using method on a program
 
Similar Threads
Encapsulation in Java
need help wtih this code!
Java Classes
variables not found with OOP
Missing Output While Incorporating A Class