Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » FundamentalRSS Feeds

Public Class

Posted By: Sara Hughes     Category: Java     Views: 4290

This article explains about public class with example in java.

Public class can be accessed by any other class in an application. Public class can accessed by any other class. If this keyword is missing, access to the class is limited to the current package. 

Syntax of public class

public class clsName
{
      // body of class
}


Examples of public class

Example 3 : Program that illustrates public class in java using DataStructure class

public class DataStructure // you can use this class instance in any package
{
    private final static int size = 15;
    int[] arrOfNum = new int[size];
    
    public DataStructure() 
    {
        for (int i = 0; i < size; i++) 
        {
            arrOfNum[i] = i;
        }
    }
    
    public void display() 
    {    
        for (int i = 0; i < size; i++) 
        {
            System.out.println(arrOfNum[i]);
        }
    }
}

class PublicClassDemo 
{
    public static void main(String args[]) 
    {
        DataStructure dsObj = new DataStructure();
        ds.display();
    }
}


Output
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14

  
Share: 

 
 
 

Didn't find what you were looking for? Find more on Public Class Or get search suggestion and latest updates.

Sara Hughes
Sara Hughes author of Public Class is from London, United Kingdom.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!