Hot

Sharly

Friday, August 23, 2013

Class Variables



A class variable exists in only one place. It keeps a single value for the class  and all its object instance . Class variables are useful to keep  class wide information. (Information  which is common to the class and all its objects)


Ex;

Public class familyMember
            {
                        Private string FirstName;
                        Private int Age;
                        Private static string Surname = “Perera”
                        …………..
.                       ………..
            }

Member 1                                Member 2

FirstName  Ajith                       FirstName Amal
Age  27                                    Age  30
Surname  Perera                       Surname  Perera



Visibility scope of methods and variables

1.         Public

            Public methods and variables can be access from any class


2.         Protected

Protected variables and methods can be access by the defining class and the its sub classes only. In java protected variables and methods cab be access by the defining class, any class  within the package (a package is a group of related classes) and sub classes any where


3.         Private

Private variables and methods can be  access by the methods of the defining class only, to support encapsulation, instance variables are normally declared private. Methods are normally declared public. But a method can be private when it performs a support role to another method and has no direct relationship to the outside world.


4.         Package/default

This is a scope  available in java. Methods and variables  declared as a package can be access by any class within the package.

No comments:

Post a Comment