= new ;
But sometimes you place some variables as arguments with classname attached with keyword "new" like:
Mydate obj1=new Mydate (22, 7, 1964);
Using the keyword "new" cause the following:
First, the space for new object is allocated and initialized to the form of 0 or null. In the java programming language, this phase is invisible to ensure that you cannot have an object with random values in it.
Second, any explicit initialization is performed.
Third, a constructor, which is a special method, is executed. Arguments passed in the parentheses to new be passed to the constructor (22, 7, 1964).
Finally, the return value from the new operation is a reference to the new object in heap memory. This reference is stored in the reference variable.
Memory allocation and layout:
In the method body, the following declaration allocates storage only for the reference as
Mybirth ????
The word new in the following example implies allocation and initialization of storage as:
Mybirth ????
Day 0
Month 0
Year 0
Explicit attribute initialization:
If you place simple assignment expressions in your member declarations, you can initialize members explicitly during construction of your object. In the Mydate class in this example, initializing all the three attributes explicitly declared, as:
Mybirth ????
Day 1
Month 1
Year 2000
Explicit initialization
Executing the constructor:
The final stage of initializing a new object is to call the constructor. The constructor enables you to override the default initialization. You can perform computations. You can also pass arguments into the construction process so that the code that requests the construction of the new object can control the object it creates.
The following example calls the constructor as like below:
Mydate obj1= new Mydate (22, 7, 1964);
Mybirth ????
Day 22
Month 7
Year 1964
CONSTRUCTOR CALL
Assigning a variable:
The variable assignment then sets up the reference variable obj1 in this example, so that it refer properly to the newly created object as shown below:
Mydate obj1=new Mydate (22, 7, 1964);
Mybirth 0x01abcdef
Day 22
Month 7
Year 1964
reference variable assignment
Published by Harsh Gupta - Tech Writer
I am a part time freelancer and writing is my hobby Some of my websites: http://www.GenericArticles.com http://www.JailBreakingiPhone.com View profile
- Object Oriented Programming - The Growing NeedIn today's competitive world, the need of the hour is to develop simple and speedy solutions that can be instantly applied to varied requirements.
- Difference Between Java and C Plus Plus (C++)There is a lot of difference between C++ and Java but there has been a misconception among several people that both Java and C++ are object oriented language but this is a misconception...
- Understanding the Structure of a JAVA ProgramJava is a high level, general purpose, object oriented language developed by Sun Microsystems of USA in 1991.
Understanding JavaA reference guide to all the people who need to learn or understand Java before they begin.- The Fundamentals of C++A guide to c++ programming.
- The Function of SQL: Databases Constructed with Object-Oriented Languages
- Types of Constructors in Object Oriented Language
- Features of Object Oriented Programming
- Advantages of Object-Oriented Programming
- JAVA - What You Need to Know About It
- Features of Java: First Platform Independent Language
- PHP Object Oriented Programming OOP for Beginners
- Java is a pure object oriented, general purpose and high level language.
