Results 1 to 3 of 3

Thread: problem with non-static variable

  1. #1

    Thread Starter
    Lively Member fundean's Avatar
    Join Date
    Apr 2001
    Posts
    98

    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());
    }
    }
    }

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Please use the code tags. It just makes it eaiser to read. Correct. A non-static variable cannot be accessed from a static context.

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Code:
     interface WelcomeMessage{
      String getWelcomeMessage();
     }
    
     class WelcomeEnglish implements WelcomeMessage{
      public String getWelcomeMessage(){
        return "Hello";
      }
     }
    
     class WelcomeSpanish extends WelcomeEnglish{
      public String getWelcomeMessage(){
       return "Hola";
     }
    }
    
     class WelcomeFrench implements WelcomeMessage{
      public String getWelcomeMessage(){
      return "Bonjour";
     }
    }
    
     public class welcome{
       public static void main(String args[]){
       WelcomeEnglish[] welcomeenglish = {new WelcomeSpanish()};
       WelcomeMessage[] welcomemessage = {new  WelcomeEnglish(),new WelcomeSpanish(), new WelcomeFrench()};
    
      for(int i=0; i<welcomeenglish.length; i++){
       System.out.println(welcomeenglish[i].getWelcomeMessage());
      }
      for(int i=0; i<welcomemessage.length; i++){
       System.out.println(welcomemessage[i].getWelcomeMessage());
      }
     }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width