|
-
Sep 9th, 2004, 09:28 AM
#1
Thread Starter
Lively Member
problem with non-static variable
//The following error occurs when I try compiling the program:
//"non-static variable cannot be referenced from static context"
//The error occurs on lines beginning with
// messages[0] = new WelcomeEnglish();
//messages[1] = new WelcomeSpanish();
//messages[2] = new WelcomeFrench();
// can anyone suggest a solution? TIA
public class welcome
{
public interface WelcomeMessage
{
String getWelcomeMessage();
}
public class WelcomeEnglish implements WelcomeMessage
{
public String getWelcomeMessage()
{
return "Hello";
}
}
public class WelcomeSpanish implements WelcomeMessage
{
public String getWelcomeMessage()
{
return "Hola";
}
}
public class WelcomeFrench implements WelcomeMessage
{
public String getWelcomeMessage()
{
return "Bonjour";
}
}
public static void main(String args[])
{
WelcomeMessage messages[];
messages = new WelcomeMessage[3];
messages[0] = new WelcomeEnglish();
messages[1] = new WelcomeSpanish();
messages[2] = new WelcomeFrench();
for(int i=0; i<3; i++)
{
System.out.println(messages[i].getWelcomeMessages());
}
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|