Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By JosAH

Thread: Class Interface or enum expected

  1. #1
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default Class Interface or enum expected

    Hi Mentors,

    I am getting the following error in my program:-

    "Class Interface or enum expected"

    Please help me with this error.

    Best Regards,
    Ankit

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default Re: Class Interface or enum expected

    Could you please show your code. Just looking at what you have posted, quite difficult to comment.

  3. #3
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default Re: Class Interface or enum expected

    Hi Eranga,

    Actually I have figured out the error now, I was by mistake using "Class" instead of "class" for defining the class interface.

    But still I want to know all other possible causes of the error.

    It would be very nice of you, if you put some light onto this

    Thanks in advance
    Best Regards,
    Ankit

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default Re: Class Interface or enum expected

    Quote Originally Posted by ankiit View Post
    Actually I have figured out the error now, I was by mistake using "Class" instead of "class" for defining the class interface.

    But still I want to know all other possible causes of the error.

    It would be very nice of you, if you put some light onto this
    Syntactically only three different things can be defined at the top level of a source text: a class, an interface or an enum (all three are types in Java). Anything else makes the compiler complain as you have noticed.

    kind regards,

    Jos
    ankiit likes this.
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default Re: Class Interface or enum expected

    Quote Originally Posted by ankiit View Post
    Hi Eranga,

    Actually I have figured out the error now, I was by mistake using "Class" instead of "class" for defining the class interface.

    But still I want to know all other possible causes of the error.

    It would be very nice of you, if you put some light onto this

    Thanks in advance
    Best Regards,
    Ankit
    Jos give you a nice explanation on his last post.

  6. #6
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default Re: Class Interface or enum expected

    Thanks a lot Jos :), I will work accordingly.

  7. #7
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default Re: Class Interface or enum expected

    Hi All,

    If i have declared a function in my class that has void as it's return type then can i use it in System.out.println(""+ fun()) ??

    Here is the sample code..

    Java Code:
    class Box
    {
        double width;
        double height;
        double depth;
    
    	//Method declaration
    
    	void vol()
    
    	 {
    	  
    	 	System.out.println(width*height*depth)	;
    		
    	 }
    
    }
    
    class BoxDemo
    {
    
    	public static void main(String[] args)
    	{
    	 Box mbox1 = new Box();
    	 Box mbox2= new Box();
    
    /* Assigning  values to the instance variable*/
    
    	mbox1.width=10;
    	mbox1.height=20;
    	mbox1.depth=15;
    
    	mbox2.width=3;
    	mbox2.height=6;
    	mbox2.depth=10;
    
    /* Calculating volumes*/
    
    	System.out.print("the volume of box1 is:=" + mbox1.vol());
    
    	System.out.print("the volume of box1 is:=");
    	mbox2.vol();
    
    
    	
    	}
    
    }
    Here is the error:-

    D:\JAVA\Practice>javac BoxDemo.java
    BoxDemo.java:39: 'void' type not allowed here
    System.out.print("the volume of box1 is:=" + mbox1.vol());

    Please suggest.

    Thanks
    Ankit
    Last edited by pbrockway2; 12-22-2011 at 06:13 AM. Reason: code tags added

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Class Interface or enum expected

    If i have declared a function in my class that has void as it's return type then can i use it in System.out.println(""+ fun()) ??
    The compiler is saying "no": 'void' type not allowed here.

    Have vol() return width*height*depth instead of printing it. And change its name to getVol().

    Or leave it as it is, but change the name to printVol() and say:

    Java Code:
    System.out.print("the volume of box1 is:=");
    mbox1.printVol();
    The first approach is preferable because returning the volume will be useful in lots of contexts not just printing it.

  9. #9
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default Re: Class Interface or enum expected

    Hi pbrockway2,

    Thanks a lot for the info.

    I have made the changes suggested by you :), now the program is working fine and yes i have changed the name of the function to "getVol()".

    Thanks
    Ankit

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

Similar Threads

  1. class interface or enum expected Imports java.io.*; error
    By prathamkool in forum New To Java
    Replies: 2
    Last Post: 10-02-2011, 05:53 PM
  2. Replies: 2
    Last Post: 10-02-2010, 03:13 AM
  3. class, interface or enum expected?
    By Addez in forum New To Java
    Replies: 2
    Last Post: 08-26-2009, 08:54 PM
  4. Replies: 1
    Last Post: 05-31-2009, 11:57 PM
  5. Replies: 9
    Last Post: 08-08-2007, 08:16 PM

Posting Permissions

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