0

Here's the code :

public class EmployeeTest { public static void main(String args[]){ //System.out.println("hello world"); Employee aEmployee = new Employee("David",1000); System.out.println(aEmployee.getName() + aEmployee.getSalary()); } } class Employee // **why can't I put a "public" here** { // Constructor public Employee(String name, double salary) { this.name = name; this.salary = salary; } // Methods public String getName() { return this.name; } public double getSalary() { return this.salary; } // instance field private String name; private double salary; } 

My question is : in the second class definition's first line, why can't I put a "public" to define it ? What's the exactly meaning of "public" when using it defines a class ?

1
  • You can put a public there if it's in a file called Employee.java Commented Aug 15, 2012 at 9:26

4 Answers 4

2

This is language feature. There must be only one top-level public class per .java file and public class name must match the source java file name.

Basically, non-public types are not accessible outside the package so if you wish to allow type to be used anywhere then make it public.

Never create a type in default package. (Always use package)

Employee.java


package com.abc.model; public class Employee{..} 

EmployeeTest.java


package com.abc.test; public class EmployeeTest{ ... } 
Sign up to request clarification or add additional context in comments.

Comments

2

Because a Java source file can have at most one top-level public class or interface, and the name of the source file must be the same as the name of that class or interface.

That's a rule that the Java compiler of Oracle's JDK imposes.

2 Comments

Can you please prove evidence for the "at most one" part? Ive searched the spec over and over and havent found such restriction. Besides that Employee is a top-level class unaffected whether its public or not.
A source file can have zero or one top-level public class or interface. It's not part of the language spec (it's not a language feature as AVD says); it's a restriction in Oracle's Java compiler.
2

In Java, there can only be a single public top level class per source file and it needs to be named the same as the file.

This is useful for the compiler when it needs to locate a class definition from outside the package, since it knows the type name, it knows which class file to find the class in. For example. since a jar file is in essence a zip file with class files, this prevents the compiler from having to unzip the entire jar to find a class definition.

The Java language specification §7.6 specifies this as an optional restriction;

If and only if packages are stored in a file system (§7.2), the host system may choose to enforce the restriction that it is a compile-time error if a type is not found in a file under a name composed of the type name plus an extension (such as .java or .jav) if either of the following is true:

  • The type is referred to by code in other compilation units of the package in which the type is declared.

  • The type is declared public (and therefore is potentially accessible from code in other packages).

Comments

0

you can define a public class inside a public class which is legal.

public class EmployeeTest { public class Employee { } } 

2 Comments

@joshz, yes you can do like this
Well that is not going to work in his specific case. The inner class must be static.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.