Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Error: Class,interface or enum expected

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Error: Class,interface or enum expected

    Hello Every 1;

    I am trying to compile the following simple code and receiving the error:

    Class Lamp{

    //define behaviour

    int brightness = 0;

    void changebrightness(int newvalue){
    brightness = newvalue;
    }

    }

    void printstates(){
    system.out.println(" Brightness: " +brightness);
    }

    Class calltomethod{
    public static void main ( string[] args) {
    // Create objects for Lamp Class

    Lamp lamp1 = new Lamp();
    // call to method using object

    lamp1.changebrightness(10);
    lamp1.printstates();

    }

    }

    Java Error.JPG

    Can someone please answer how to fix the above error ?

    Thanks,
    FJ


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Error: Class,interface or enum expected

    You see in your image where it tells you the error is on line 1, and then on the next line there's a '^'? That '^' points to the character on the problematic line where javac thinks things have started going wrong. Remember that Java is CaSe SeNsItIvE and that all its keywords are lower-case.

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Error: Class,interface or enum expected

    Thank you Sean4u,

    I somehow managed to get rid off these errors.... but I cannot understand the below error where it says couldnot load the main class (class name.java). I also got the same error in my previous program which I was unable to run.

    thank you for looking into this.

    MainClass Error.JPG

  4. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Error: Class,interface or enum expected

    Whoopsie - I'm blind as a bat.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  5. #5
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Error: Class,interface or enum expected

    Put your main method outside of the inner class
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  6. The Following User Says Thank You to newbie For This Useful Post:

    FJIW (September 18th, 2011)

  7. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Error: Class,interface or enum expected

    I'm thinking it's a bracket where it shouldn't be.

    Yep, it looks like your class ends, unless that was an inner class.

    Class Lamp{

    //define behaviour

    int brightness = 0;

    void changebrightness(int newvalue){
    brightness = newvalue;
    }

    } <----------------------

    perhaps you should put printstates() before that bracket indicated above.

    I think right now it thinks it's a class as it's defined outside of any class.

    By any chance are Lamp and calltomethod two separate classes?
    Last edited by javapenguin; September 17th, 2011 at 09:47 PM.

  8. #7
    Junior Member
    Join Date
    Sep 2011
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Error: Class,interface or enum expected

    Ok please pardon my lack of knowledge here. I tried the instructions but seems like I am doing something wrong. I m still receiving the same error could not find or load the main class PrgLamp.

    class Lamp{

    //define behaviour

    int brightness = 0;

    void changebrightness(int newvalue){
    brightness = newvalue;

    }

    void printstates(){
    System.out.println(" Brightness: " +brightness);

    }


    class calltomethod{

    public void main(String[] args) {


    Lamp lamp1 = new Lamp();
    lamp1.changebrightness(10);
    lamp1.printstates();

    }

    }

    }

    Thanks,
    FJ

  9. #8
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Error: Class,interface or enum expected

    You should always copy and paste your full errors, rather than re-typing them. It looks to me as though you might be trying to compile a file called "PrgLamp.java" which only contains a class called "Lamp" - is that what you're doing? PrgLamp.java must contain a class called PrgLamp. It can *also* contain a class called Lamp, but class PrgLamp must be in there somewhere.

  10. #9
    Junior Member
    Join Date
    Sep 2011
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Error: Class,interface or enum expected

    Hi Sean4u,

    I compiled the filename PrgLamp.java and it compiled without errors. When I try to run the code using following command java PrgLamp

    it throws below error:Could not find or load main class PrgLamp.java.

    There is no class called "PrgLamp" in the file. So is it the case that whatever the file name is, there should be always be a class name equal to the the name of file ? Should I rename the class Lamp to PrgLamp and that will fix the problem ?
    Thanks,
    FJ
    Last edited by FJIW; September 18th, 2011 at 02:23 PM.

  11. #10
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Error: Class,interface or enum expected

    It might be easier to rename your file. You refer to Lamp in several places, but you'd only have to change the filename once. Give it a try and let us know where it gets you.

  12. The Following User Says Thank You to Sean4u For This Useful Post:

    FJIW (September 18th, 2011)

  13. #11
    Junior Member
    Join Date
    Sep 2011
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Error: Class,interface or enum expected

    Ok here what I did,
    Renamed the program to Lamp. Now the program name and the class name is same.
    Removed the calltomethod class as it was not required. Compiled the c
    class Lamp{

    //define behaviour

    int brightness = 0;

    void changebrightness(int newvalue){
    brightness = newvalue;

    }

    void printstates(){
    System.out.println(" Brightness: " +brightness);

    }

    public static void main(String[] args) {

    Lamp lamp1 = new Lamp();
    lamp1.changebrightness(10);
    lamp1.printstates();

    }


    }

    And it works now.

    I thank you Sean and everyone for being so responsive. This place is great for newbies.

    FJ.

Similar Threads

  1. .'class' expected - what to do?!?! URGENT
    By Rads23 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 17th, 2011, 06:05 PM
  2. Trouble using enum in constructor when creating a class
    By willmer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 13th, 2011, 10:48 AM
  3. Replies: 1
    Last Post: June 13th, 2011, 07:02 PM
  4. interface class ActionListener
    By NightFire91 in forum Java Theory & Questions
    Replies: 2
    Last Post: August 17th, 2010, 10:42 AM
  5. what is the use of transient class and serializable interface?
    By chinni in forum Object Oriented Programming
    Replies: 3
    Last Post: October 28th, 2009, 05:48 PM