aspose file tools
The moose likes Beginning Java and the fly likes Can we declare more than one public class in single .java file Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Can we declare more than one public class in single .java file" Watch "Can we declare more than one public class in single .java file" New topic
Author

Can we declare more than one public class in single .java file

bunty wadekar
Greenhorn

Joined: Jan 24, 2007
Posts: 1
public abstract class Person {

protected int dollars;
protected int friends;

public void makeDollar ( ){
dollars++;
}

public void makeFriend (){
friends++;
}

/* method that calculates level of happiness;
to be implemented by subclasses ( differnet
kinds of people derive their happiness from
different things ) */

public abstract int getHappiness();

}

public class GreedyPerson extends Person {

public int getHappiness() {
int happinessValue;
int moneyWeight = 10;
int friendsWeight = 1;

happinessValue = (moneyWeight * dollars) +
( friendsWeight * friends);

return happinessValue;
}
}

public class SocialPerson extends Person {

public int getHappiness() {
int happinessValue;
int moneyWeight = 1;
int friendsWeight = 10;

happinessValue = (moneyWeight * dollars) +
( friendsWeight * friends);

return happinessValue;
public static void main(String[] args){}


}
}
I got the follwing error ::

SocialPerson.java:1: class Person is public, should be declared in a file named
Person.java
public abstract class Person {
^
SocialPerson.java:23: class GreedyPerson is public, should be declared in a file
named GreedyPerson.java
public class GreedyPerson extends Person {
^
2 errors
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24057
    
  13

No, you can't. You should put each public class in a file named after that class -- as the error messages explain rather clearly.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Can we declare more than one public class in single .java file
 
Similar Threads
How does this work ?
protected variable?
Use of static in abstract class..
abstract native
instance variables of Abstract Classes