Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using method on a program

 
Alfonso Saballett
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OR, initialize them at the time of declaring !

 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Alfonso Saballett
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic