5 - Introduction To Class, This, Static Etc in Java PDF
5 - Introduction To Class, This, Static Etc in Java PDF
5.1 Objective
Introduction to Classes
Declaration of Classes
Declaration of instance variables
Declaration of methods
Object and Object creation
Access Modifiers
Constructors
Use of this Keyword
Examples for “this” keyword
static keyword
A class is a template, blueprint,or contract that defines what an object’s data fields and methods
will be. An object is an instance of a class. You can create many instances of a class.
A Java class uses variables to define data fields and methods to define actions. The attributes and
behaviours defined by a class are common to all objects of the same kind.
A class is declared by the use of the “class” keyword. Class body is enclosed between curly
braces { and }. The data or variables defined within the class are called instance variables. The code is
contained within methods. Collectively, the methods and variables defined in a class are called
members of the class.
TCS Internal
Here we can say there is a class Car.
TCS Internal
TCS Internal
A class can contain any of the following variable types.
TCS Internal
Local variables: Variables defined inside methods, constructors or blocks are called local variables.
The variable will be declared and initialized within the method and the variable will be destroyed when
the method has completed.
Instance variables: Instance variables are variables within a class but outside any method. These
variables are instantiated when the class is loaded. Instance variables can be accessed from inside any
method, constructor or blocks of that particular class.
Class variables: Class variables are variables declared with in a class, outside any method, with the
static keyword
Figure 1
Variables defined within a class are called instance variables because each instance of the class (ie,
each object of the class) contains its own copy of these variables. Thus, the data for one object is
seperate and unique from the data of another. Instance variables can be declared public or private or
default (no modifier). When we do not want our variables value to be changed outside our class we
should declare them private. Public variables can be accessed and changed from outside the class. We
will have more information about this on OOP tutorial.
TCS Internal
5.2.4 Declaration of Methods
A method is a program module that contains a series of statements that carry out a task. To execute a
method, you invoke or call it from another method; the calling method makes a method call, which
invokes the called method. Any class can contain an unlimited number of methods, and each method
can be called an unlimited number of times.
The syntax for method declaration is as given below. Java main method is given as an example.
TCS Internal
Consider Tiger from the above list,
TCS Internal
Objects are made up of attributes(properties) and methods(behaviours). Attributes are the
characteristics that define an object; the values contained in attributes differentiate the objects of the
same class from one another.
TCS Internal
We can say --- Object is an instance of a class.
Consider an object Car. What we know about a car defines its attributes. What a car can
do defines its behaviours/methods.
TCS Internal
Always operations can alter the state of the attributes.
Consider the above start operation for a Car. When the car is started ,it is affecting the state of the
engine.
TCS Internal
Creating an Object:
As mentioned previously, a class provides the blueprints for objects. So basically an object is created
from a class. In Java, the new key word is used to create new objects.
TCS Internal
There are three steps when creating an object from a class:
Initialization: The 'new' keyword is followed by a call to a constructor. This call initializes the new
object.
We are creating three objects truck, bike and a car from Vehicle class.
TCS Internal
Accessing Instance Variables and Methods using Objects:
TCS Internal
/* call a variable as follows */
ObjectReference.variableName;
TCS Internal
5.2.6 Constructors
Java constructors are special methods which are used to initialize objects. Constructor method
has the same name as that of class, they are called or invoked when an object of class is created and
can't be called explicitly. They are designed to perform initializing actions such as initializing the data
fields or objects.
A java constructor has the same name as the name of the class to which it belongs. Constructor’s syntax
does not include a return type, since constructors never return a value.
Constructors can be classified into two types, default constructors and parametrized constructors.
If you don't define a constructor, then the compiler creates a default constructor. Default constructors
do not contain any parameters. Default constructors are created only if there are no constructors defined
by us. Java provides a default constructor which takes no arguments, when no explicit constructors are
provided. Parametrized constructors are required to pass parameters on creation of objects.
TCS Internal
5.2.7 Access Specifiers
Each object has members (members can be variables or methods) which can be declared to have
specific access.
TCS Internal
Below program will give compilation Error.
TCS Internal
5.2.8 Use of this Keyword
There can be a lot of usage of this keyword. In java, this is a reference variable that refers to the
current object.
TCS Internal
5.2.9 Example on this keyword
If there is ambiguity between the instance variable and parameter, this keyword resolves the
problem of ambiguity.
Let's understand the problem if we don't use this keyword by the example given below:
TCS Internal
In the above example, parameter (formal arguments) and instance variables are same that is why we are
using this keyword to distinguish between local variable and instance variable.
TCS Internal
TCS Internal
Solution of the above problem by with out using this keyword
TCS Internal
5.2.10 Static Keyword
Static variables are also known as Class variables. Static variables are declared with the static
keyword in a class, but outside a method, constructor or a block.
There would only be one copy of each class variable per class, regardless of how many objects are
created from it.Static variables are stored in static memory.
Static variables are created when the program starts and destroyed when the program stops.
Visibility is similar to instance variables. However, most static variables are declared public since they
must be available for users of the class.
Default values are same as instance variables. For numbers, the default value is 0; for Booleans, it is
false; and for object references, it is null. Values can be assigned during the declaration or within the
constructor.
Static variables can be accessed by calling with the class name . ClassName.VariableName.
Consider the below class Vehicle, here manufacturteName is declared as static and
getManufactureName() method also declared as static.
TCS Internal
Output
2. Member of a class having same name as class that helps in the construction of the object
a. Start method
b. Constructor
TCS Internal
c. Function
d. Construct
Ans: b
6. Modifier to prevent any method or attribute to be made invisible from outside object.
a. Private
b. Public
c. Protected
d. Default
Ans: a
7. Modifier to prevent any method or attribute to be made visible for any outside object.
a. Private
b. Public
c. Protected
d. Default
Ans: b
TCS Internal
TCS Internal