Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

why can I not create an instance in a method, but I can in main??

Status
Not open for further replies.

Oxymoron

Technical User
Dec 17, 2000
168
GB
Hi.
I'm new to java, and am trying a few easy projects; but I just cannot get this code to work.
I am trying to create an instance of an athlete (Racer), assign him a name through the constructor, then create two instances of Time (which is a seperate class), and assign these instances of time to the instnce of Racer as 'start' and 'finish' times.
The odd thing is, I can do this fine inside main, but when I try to call a method from the instance of Race to do all this assigning (as a good java practice) it says "cannot resolve symbol r1' which is my Race instance.
Why does it let me do it in the main method, but not anywhere else????????

Please, if someone can help, I'd be VERY grateful!!

-----------------------Time.java ------------------------
/**
*@author: Joe Wardell
*@Worksheet: 4
*@Date: 21/10/02
*/

public class Time
{ //declare attributes:
private int timeInSecs;

//constructor; used to initialise the values of Time needed
public Time(int hours, int minutes, int secs)
{
timeInSecs = (hours * 3600) + (minutes * 60) + (secs);
}

//declare the GET methods:

public int getHoursElement()
{
return timeInSecs / 3600;
}
//------------------------------
public int getMinutesElement()
{
return (timeInSecs % 3600) / 60;
}
//-------------------------------------------
public int getSecsElement()
{
return timeInSecs % 60;
}
//--------------------------------------------------------

//method to manipulate the main attribute...
//timeInSecs

public void increment(int hours, int minutes, int secs)
{
timeInSecs = timeInSecs + (hours * 3600) + (minutes * 60) + (secs);
}

public String toString()
{
return "Time is:\n" +
"\t" + getHoursElement() + " hours\n" +
"\t" + getMinutesElement() + " minutes\n" +
"\t" + getSecsElement() + " secs\n";
}

public static void main(String[] args)
{
Time t1 = new Time(10, 20, 30);
t1.increment(1, 10, 50);
System.out.println(t1);
}

}//end of class Time
-----------------------------------------------------------
-------------------------Race.java-------------------------
/**
*@author: Joe Wardell
*@Worksheet: 4
*@Date: 21/10/02
*/

public class Race
{
//declare attributes:
public String name;
public Time start;
public Time finish;

/**constructor for Race
*@param theName takes the new instances name
*/
public Race(String theName)
{
name = theName;
}

//declare the SET and GET method for attribute; name
public void setName (String theName)
{
name = theName;
}
//----------------------------------
public String getName()
{
return name;
}
//----------------------------------
public void setStart (Time theStart)
{
start = theStart;
}
//----------------------------------
public Time getStart()
{
return start;
}
//----------------------------------
public void setFinish (Time theFinish)
{
finish = theFinish;
}
//----------------------------------
public Time getFinish()
{
return finish;
}
//----------------------------------
public void raceDemo()
{
//TimeRace t1 = new Time(10, 30, 21);
//TimeRace t2 = new Time(11, 40, 50);
//r1.setStart(t1);
//r1.setFinish(t2);
//System.out.println(r1);

//All this commented area is where the
//code would be, if calling this method worked!

}

public static void main(String[] args)
{
Race r1 = new Race("Joe Wardell");
r1.raceDemo();
Time t1 = new Time(10, 30, 21);
Time t2 = new Time(11, 40, 50);
r1.setStart(t1);
r1.setFinish(t2);
System.out.println(r1);
}

public String toString()
{
return "Racer Name: " + getName() + "\n"
+ getStart();
}

}//end of class Race
-----------------------------------------------------------
Any and ALL suggestions r very welcome!
Cheers!
JoE we are all of us living in the gutter.
But some of us are looking at the stars.
 
Um, I'm pretty new to Java aswell, but I think it is because you r1 object is instantiated in main() - thus it cannot be used anywhere else. Try making you main() to this:

Race r1;
public static void main(String[] args)
{
r1 = new Race("Joe Wardell");
r1.raceDemo();
Time t1 = new Time(10, 30, 21);
Time t2 = new Time(11, 40, 50);
r1.setStart(t1);
r1.setFinish(t2);
System.out.println(r1);
}

ie. Take Race and make it global... I'm pretty sure this would work (I think). napa1m
hansen@ithsite.com
 
napa1m is right : declaring r1 into main method reduces its scope to this only method. Declare it outside main to increase its scope to all the class. Note : you can use scope modifiers like "public", "private" and "protected" to change the scope of your variable outside the class. The default (if you don't supply any scope modifier) is "protected". In this case, the variable is accessible in all the classes of the same package. "private" make it visible only into the class, while "public" make it accessible everywhere to every class. Water is not bad as long as it stays out human body ;-)
 
Do you go to Aberystwyth uni? I do and i remeber doing that worksheet last year. Declare r1 as a variable at the begining of the class then instansiate it wherever you want, you'll always have access to it.
 
wow, small world. Yes I do, I posted the post then found if I created the instance outside of main, then it's scope was non-static and could be used throughout any methods in the class, unlike main.
Enjoy the 2nd year! we are all of us living in the gutter.
But some of us are looking at the stars.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top