Home can i define more than one public class in a java package
Reply: 2

can i define more than one public class in a java package

Adarsh D
1#
Adarsh D Published in 2018-02-13 10:36:00Z

i want to create a java package containing some classes .But non public classes cannot be accessed from outside a package so i write this code a saved it as Box.java

package mypack;
public class Box
{
int length,breadth,height;
public Box(int l,int b,int h)
    {
    length=l;
    breadth=b;
    height=h;
    }
public  int vol()
    {
    return length*breadth*height;
    }
} 

public class circle
{
 int r;
 public circle(int rr)
    {
    r=rr;
    }
 public float area()
    {
    return (3.14f*r*r);
    }
 }  

is it possible to create a package like this . if possible what file name should i give it 'Box.java' or 'circle.java'

Przemysław Moskal
2#
Przemysław Moskal Reply to 2018-02-13 10:55:10Z

There can be more than one public class in package, but each public class needs to be declared in its own .java file. If you want these public classes that are in different .java files to be contained in the same package, simply put package mypack; at the very beginning of these .java files. This is the example, how you should name these files:

  1. Box.java:

    package mypack;
    public class Box {...}
    
  2. Circle.java:

    package mypack;
    public class Box {...}
    

Note that files should be named with name of public class contained in it (if public class is present inside).

By the way, be aware of naming conventions in Java language - it might save your and others time when looking at code - class names should start with capital letter, so in your case it should be Circle class and Circle.java, not circle class and circle.java.

developer
3#
developer Reply to 2018-02-13 10:54:36Z

You can only create only one public type (like a class/interface, etc..) inside a source file as explained below from Java documentation (emphasis is mine).

To create a package, you choose a name for the package (naming conventions are discussed in the next section) and put a package statement with that name at the top of every source file that contains the types (classes, interfaces, enumerations, and annotation types) that you want to include in the package. If you put multiple types in a single source file, only one can be public, and it must have the same name as the source file.

I suggest you read here on how to create and manage packages and source/class files in java.

Also, note that you are allowed to contain more than one non-public (default access) types inside a single source file as shown below:

Inside your Circle.java File:

package mypack;

class Box { //non-public (default) class
 //code for Box class
} 

public class Circle {//public class
  //code for circle class
} 

Also, inside a package, you can create any number of public classes (by following above rule). But, as a good practice, group your classes properly into various packages according to their relevance.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.316106 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO