Results 1 to 4 of 4
  1. #1
    stampa is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default Getting illegal start of expression in main

    class Mug{

    Mug(int marker)
    {
    System.out.println("mug ("+ marker +")");

    }
    public void f(int marker)
    {
    System.out.println("f(" +marker+ ")");
    }


    }

    class Mugs{

    static Mug m1;
    static Mug m2;

    static{
    m1 = new Mug(1);
    m2 = new Mug(2);
    }
    //m1.f(1);
    //m2.f(2);

    Mugs()
    {
    System.out.println("the Mugs object is being created");
    }
    void print()
    {
    System.out.println("initializing mugs");
    }
    }


    /**
    *
    *
    */
    public class NonStaticInit {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {

    static Mugs mm1 = new Mugs();


    }
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    You cannot have static there.

  3. #3
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    382
    Rep Power
    3

    Default

    To be more precise... Static variables and objects are only used outside of method declarations, never inside of them. So remove the "static" from Mugs mm1=new Mugs(); and you should be fine. Assuming the java-file is called NonStaticInit.java, that is.

  4. #4
    stampa is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

Similar Threads

  1. Illegal start of expression
    By lodaSchitt in forum New To Java
    Replies: 2
    Last Post: 04-28-2011, 10:04 PM
  2. Illegal Start of Expression
    By Tyre in forum New To Java
    Replies: 20
    Last Post: 04-15-2011, 02:50 PM
  3. Illegal start of expression
    By Tekila in forum New To Java
    Replies: 10
    Last Post: 03-04-2011, 12:23 AM
  4. Illegal start of expression
    By Basit56 in forum New To Java
    Replies: 2
    Last Post: 08-18-2009, 09:12 AM
  5. Illegal Start of Expression
    By vince425 in forum New To Java
    Replies: 3
    Last Post: 10-18-2008, 07:41 AM

Posting Permissions

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