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 ?

joshz
  • 133
  • 2
  • 10

4 Answers4

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.

Jesper
  • 202,709
  • 46
  • 318
  • 350
  • 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. – Sebastian Hoffmann Aug 15 '12 at 09:52
  • 1
    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. – Jesper Aug 15 '12 at 09:54
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{ ... }
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
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).

Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
0

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

public class EmployeeTest
{
     public class Employee {

     }
}
UVM
  • 9,776
  • 6
  • 41
  • 66