This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Class declaration problem. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Class declaration problem." Watch "Class declaration problem." New topic

Class declaration problem.

Anu satya
Ranch Hand

Joined: Mar 17, 2005
Posts: 146
I am writing one test application where, employee name and salary are displayed. After that, e-sal has to me incremented to 10% on current salary and display the output like this.....
Employ 1:Bob Jon s;Yearly Salary:34500.00 Employ 2:Susan Bak r;Yearly Salary:37809.00
Incr asing employee salari s by 10 Employ 1:Bob Jon s;Yearly Salary:37950.00 Employ 2:Susan Bak r;Yearly Salary:41589.90
I must you another class by name "Employeetest". My code as follows

import java.util.*;

public class Employee
private String firstname="";
private String lastname="";
private double monthsal=0.0;

Employee(String fnm,String lnm,double sal)
//validation for negative numbers
//end constructor

public void setfname(String fnm)
public String getfname()
return firstname;

public void setlstname(String lnm)
public String getlstname()
return lastname;

public void setsal(double sal)
//validation for negative numbers
public double getsal()
return monthsal;

public void display()
//System.out.println("This is from display method");
System.out.print("New salary=");
}//end of class Employee

public class Employeetest
public static void main(String[] args)
Employee emp1=new Employee("Anu","Radha",2305.00);
Employee emp2=new Employee("Raja","Kumar",4599.05);
System.out.print("Employee1: ");
System.out.println("Employee2: ");

Error message:
C:\Ash\javaprms>javac class Employee is public, should be declared in a file name
public class Employee
1 error

Can anybody help me in this regard:

1. HOw to use Employee class from Employeetest.
2. How to display incremented salary after displaying current salary ?

With Regards,

Joined: Oct 17, 2005
Posts: 1

You should have two separate files. One called containing the class EmployeeTest and the other called containing the employee class. That's the way it's done in Java.

There's a rule that you can only have one public class per java file. You can have more than one class, but only one public class. It's usually a good idea to stick to one class per java file.

You compile them together like this:


<a href="" target="_blank" rel="nofollow"></a>
Bhavna Jharbade
Ranch Hand

Joined: Sep 08, 2005
Posts: 69
Hi Ashvini,
The error you r getting may be because you might have not saved the
Employee class with properly.Save it as

1)You have done proper use of Employee in the Employeetest class.

2)To get the new salary make the following changes in your code
a)make the return type of setsal(double sal) method as double
n return monthsal from it

b) Include the extra line in the display() method as shown below

Hope this solves your problem
Gud luck

SCJP1.4 (86%)
Aadi Narayana Reddy

Joined: Oct 17, 2005
Posts: 13
hi ash,

There should be only one Public Class in a java file. But u have written two public classes in the same java file. So it wont compile first.
So u just delete the public key word of the first class, and compile it.
or u can write the first class in seperate java file. and u can create object of that class from the second class.

G.Adi Narayana Reddy,
Bimal Patel
Ranch Hand

Joined: Aug 29, 2003
Posts: 130
Hi All,

I think in general, One should write whatever he/she wants to test in a class which has default access and not the public say Employee. And in the same file write a class which should have main method having public access, say EmployeeTest.

Work Hard, Expect The Worst...<br /> <br />Bimal R. Patel<br />(SCJP 1.2, SCWCD 1.4)
I agree. Here's the link:
subject: Class declaration problem.
Similar Threads
Help with Println and Scanner
Problem with @Column annotation or corresponding xml tag if used with @Id annotation
Problem with refresh() method of EntityManamger in JPA
Polymorphism doubt