Java supports a special type of methods, called constructor
that enables an object to initialize itself when it is created. Constructors
have the same name as the class it-self. Constructors do not specify a return type, not
even void. This is because they return the instance of the class itself. A
constructor is automatically called when an object is created.
Syntax:
Constructor_name([arguments])
{
// body
}
Constructors
are generally of two types.
1. Non-Parameterized
2. Parameterized
KEYWORDS
IN JAVA
We are going to learn few regularly
used keywords.
1. new
2. this
3. static
4. super
5. final
new:
The new keyword dynamically allocates
memory for an object.
Syntax:
claas_name object _name = new
class_name();
EX.
Box b1 = new Box();
Box b2 = new Box();
We have already seen so many
programs in which we used new keyword
for creating objects.
this:
This keyword is the name of a reference that refers to a calling object itself. One common use of the this keyword is to reference a class` hidden data fields. We can have local variables, including formal parameters to methods which overlap with the names of the class` instance variables. The local variable hides the instance variable so we use this keyword. Another common use of this keyword is to enable a constructor to invoke another constructor of the same class. java requires that the this(arg-list) statement appear first in the constructor before any other statements.static :
A
class member must be accessed with the use of an object of its class but
sometimes we want to define a class member that will be used independently
without creating any object of that class. It is possible in java to create a
member that can be used by itself, without reference to a specific instance. To
create such a member, precede its declaration with the keyword static. When a
member is declared static, it can be accessed before any objects of its class
are created, and without reference to any object. One can declare both methods
and variables to be static. The most common example of a static member is
main(). Main () is declared as static because it must be called before any
object exist. Instance variables declared as static are actually, global
variables. When objects of its class are declared, no copy of a static variable
is made. Instead, all instances of the class share the same static variable.
Method declared as static have
several restrictions:1. Can call only other static methods.
2. Only access static data.
3. Cannot refer to this or super in any way.
One can also declare a static block which gets executed exactly once, when the class is first loaded.
super :
super
keyword is used to call a superclass constructor and to call or access super
class members(instance variables or methods).
Syntax of
super :
=>
super(arg-list)
When
a subclass calls super() it is calling the constructor of its immediate
superclass. super() must always be the first statement executed inside a
subclass constructor.
=>
super.member
Member
can be either method or instance variables. This second form of super is most
applicable to situation in which member names of a subclass hide member of
superclass due to same name.
final :
final
keyword can be use with variables, methods and class.
=> final variables
When we want to declare constant
variable in java we use final keyword.
Syntax : final
variable name = value;
=> final method
Syntax: final methodname(arg)
When
we put final keyword before method than it becomes final method. To prevent
overriding of method final keyword is used, means final method can’t be
override.
=> final class
A
class that can not be sub classed is called a final class. This is archived in
java using the keyword final as follow. Any attempt to inherit this class will
cause an error and compiler will not allow it.
Syntax : final class class_name { ... }
Anurag
Comments
Post a Comment