Results 1 to 9 of 9

Thread: code error

  1. #1
    jovin555 is offline Member
    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Default code error

    class employee
    {
    float salary;
    String name;
    double empid;
    void data()
    {
    System.out.println("salary is="+salary);
    System.out.println("name is="+name);
    System.out.println("employee id is="+empid);

    }
    }
    public class empdata
    {
    public static void main(String args[])
    {
    employee obj=new employee();
    obj.salary =1000;
    obj.name= "A";
    obj.empid=1001;
    obj.data();
    }
    }

    when i use public before class empdata i get error but if i dont use that i get the program to run....can anyone explain me the reason?

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

    Default Re: code error

    Make sure the name of the file is "employee.java". Or even better, make the name of the class "public class Employee" in a file named "Employee.java". Also, there can be at most one public class in a source file.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    jovin555 is offline Member
    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Default Re: code error

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:

    at empdata.main(Main.java:16)

    this is my error..and i have changed the code like u said and i made sure that it is saved as .java..

    public class employee
    {
    float salary;
    String name;
    double empid;
    void data()
    {
    System.out.println("salary is="+salary);
    System.out.println("name is="+name);
    System.out.println("employee id is="+empid);

    }
    }
    public class empdata
    {
    public static void main(String args[])
    {
    employee obj=new employee();
    obj.salary =1000;
    obj.name= "A";
    obj.empid=1001;
    obj.data();
    }
    }

  4. #4
    jovin555 is offline Member
    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Default Re: code error

    ok.thank you.its working now..i changed the name of file to Empdata.java and its working...but can u tell me the reason for that...

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: code error

    Even though your IDE may (unfortunately!) allow it, don't try to run code that didn't compile.

    Post the compile errors to get help.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: code error

    Quote Originally Posted by jovin555 View Post
    ok.thank you.its working now..i changed the name of file to Empdata.java and its working...but can u tell me the reason for that...
    It's a rule, mandated by the Java Language Specification. What more reason do you want?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    jovin555 is offline Member
    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Default Re: code error

    why we cant give another name like main.java instead of Empdata.java?

  8. #8
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: code error

    Darryl already answered you question in the previous post.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: code error

    It still amazes me how people agonize over non-issues. Would you want to put a McDonald's sign over a Domino's outlet?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. What does this error code mean?
    By Kareng in forum New To Java
    Replies: 3
    Last Post: 10-26-2012, 04:37 PM
  2. eRROR with my code not sure why
    By Exceedinglife in forum New To Java
    Replies: 1
    Last Post: 04-25-2012, 07:16 AM
  3. Error in my code
    By MikeJ39 in forum New To Java
    Replies: 2
    Last Post: 10-19-2011, 09:58 PM
  4. error code help please..
    By andnlou2678 in forum New To Java
    Replies: 7
    Last Post: 10-07-2011, 12:11 AM
  5. error in this code
    By gradiente99 in forum Eclipse
    Replies: 11
    Last Post: 07-20-2010, 05:58 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
  •