Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » FundamentalRSS Feeds

Public Variable

Posted By: Alessio Smith     Category: Java     Views: 3214

This article explains about public variable in java with example.

Public variable can be accessed by any other class in any package.

Syntax of Public Variable

public type varName = value; 

type is any data type in java.
varName is any valid identifier in java. It is a variable name.
value is an optional. You can initialize variable by assigning its value. 

Example of Public Variable

Example 1 : Program that illustrates public variable use in java

class Sample
{
    public int x;
}

class PublicVariableDemo
{
    public static void main(String[] args) 
    {
        Sample sObj = new sObj();
        sObj.x = 10;

        System.out.println(sObj.x);
    }
}


Output
10
  
Share: 

 
 
 

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

Alessio Smith
Alessio Smith author of Public Variable 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!