Page 233 - Cs_withBlue_J_C11_Flipbook
P. 233
• Public: Methods and variables when declared public can be used within a class as well as outside the class. If the
access specifier is not mentioned, then by default it is taken as public.
E.g., public int a, b, s=0;
• Private: Data members and variables declared as private can be accessed only from within the scope of the class.
E.g., private int radius, area, perimeter;
• Protected: When we declare data members and methods as protected, then the members can not only be
accessed from the same class but also from the inherited class.
E.g., protected int length, breadth;
b. Class: It is a reserved word that is used to declare a class.
c. Name of the class: It is used to give a name to define the class so that it can be easily referred to when required.
d. Data Members: They are the variables described within a class and can be used to store values whenever required.
e. Member Methods: The defined methods in a class are termed Member Methods. These methods have some stored
values which are used as global variables and these variables are also used to perform some jobs.
Apart from the above mentioned, there are two parts of a class which are as follows:
a. External Wrapper: The whole class is encapsulated in a bracket. It contains the reserved word “class” and the class
name.
b. Internal Wrapper: Whatever is contained in the braces after the class name, is the Internal Wrapper. It contains the
instance variable, class variable, local variable, constructor and member methods.
• Instance Variable: It is a non-static data member that is created whenever any object is generated. All instance
variables are accessed by preceding it with the object name.
For example,
char ch;
short s;
• Class Variable: It is also known as a static data member which is capable of creating only one copy for all the
objects of the class. Thus, there would be only one copy of the variable per class, no matter the number of objects
that may be created from it.
For example,
static int a;
static double b;
Differences between ordinary and static data members are as follows:
Ordinary Data Members Static Data Members
All objects created have individual data members Only one copy of the data members is used by all the
assigned to them. objects created.
Any change in the value stored in a data member will Any change made in a static data member through an object
affect only the respective object. will affect the common data members of all the objects.
Let us see an example given below:
class counter
{
static int increase=0;
static void incre()
{
increase++;
}
void display()
{
System.out.println("Counter : "+ increase);
}
public static void main()
231
Methods and Constructors 231

