Skip to main content

CONSTRUCTOR & KEYWORDS IN JAVA



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

Popular posts from this blog

JAVA Scrollbar, MenuItem and Menu, PopupMenu

ava AWT Scrollbar The  object  of Scrollbar class is used to add horizontal and vertical scrollbar. Scrollbar is a  GUI  component allows us to see invisible number of rows and columns. AWT Scrollbar class declaration public   class  Scrollbar  extends  Component  implements  Adjustable, Accessible   Java AWT Scrollbar Example import  java.awt.*;   class  ScrollbarExample{   ScrollbarExample(){               Frame f=  new  Frame( "Scrollbar Example" );               Scrollbar s= new  Scrollbar();               s.setBounds( 100 , 100 ,  50 , 100 );               f.add(s);   ...

Difference between net platform and dot net framework...

Difference between net platform and dot net framework... .net platform supports programming languages that are .net compatible. It is the platform using which we can build and develop the applications. .net framework is the engine inside the .net platform which actually compiles and produces the executable code. .net framework contains CLR(Common Language Runtime) and FCL(Framework Class Library) using which it produces the platform independent codes. What is the .NET Framework? The Microsoft .NET Framework is a platform for building, deploying, and running Web Services and applications. It provides a highly productive, standards-based, multi-language environment for integrating existing investments with next-generation applications and services as well as the agility to solve the challenges of deployment and operation of Internet-scale applications. The .NET Framework consists of three main parts: the common language runtime, a hierarchical set of unified class librari...

Standard and Formatted Input / Output in C++

The C++ standard libraries provide an extensive set of input/output capabilities which we will see in subsequent chapters. This chapter will discuss very basic and most common I/O operations required for C++ programming. C++ I/O occurs in streams, which are sequences of bytes. If bytes flow from a device like a keyboard, a disk drive, or a network connection etc. to main memory, this is called   input operation   and if bytes flow from main memory to a device like a display screen, a printer, a disk drive, or a network connection, etc., this is called   output operation . Standard Input and Output in C++ is done through the use of  streams . Streams are generic places to send or receive data. In C++, I/O is done through classes and objects defined in the header file  <iostream> .  iostream  stands for standard input-output stream. This header file contains definitions to objects like  cin ,  cout , etc. /O Library Header Files There are...