Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » FundamentalRSS Feeds

Public Constructor

Posted By: Albert Smith     Category: Java     Views: 2445

This article explains about public constructor in java with example.

Public constructor can be invoked by any other class.

Syntax of Public Constructor

class clsName
{
     // Variable declaration
 
     public clsName()
     {
     }

     // Methods
}


clsName is a valid identifier in java. It is a class name.

Example of Public Constructor

Example 1 : Program that illustrates public constructor use in java 

class Person
{
       String name;
       int age;

       public Person(String name, int age)
       {
              this.name = name;
              this.age = age;
        }
}

class PublicConstructorDemo
{
       public static void main(String args[])
       {
                Person pObj = new Person("Michel",24);

                System.out.println(pObj.name);
                System.out.println(pObj.age);
       }
}


Output

Michel
24
  
Share: 

 
 
 

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

Albert Smith
Albert Smith author of Public Constructor is from Sydney, Australia.
 
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!