In this article,Object Class and Constructors in Java are explained in detail with examples.
It is a pre-defined class in java available in "java.lang " package.
It is a pre-defined class in java available in "java.lang " package.
This class is a Base-class or Super-class for all our user-defined classes or pre-defined classes directly or indirectly.
This class provides runtime instance for all your classes getting executed in the memory
This class contains 7-native methods.
Native methods are those methods whose implementation is done in other languages like C and C++.
The following methods are native methods in the object class. (From topic Object Class and Constructors in Java)
- RegisterNatives();
- get class();
- hashcode();
- clone();
- notify();
- notify All();
- wait(long);
- toString();
- finalize();
- equals(object); (From topic Object Class and Constructors in Java)
Constructors are special methods of a class, which have the same name as that of a class.
Ex
class A
{
data members;
----
----
Methods;
----
----
A() //construction of class A.
{
----
}
}
Constructor gets executed automatically whenever an object of a class is created or instantiated.
Ex
A obj=new A();
or
Student s1=new Student(); // student = constructor.
Actually, constructors are responsible for allocating memory for your objects.
If a class doesn't have a constructor, then JVM provides a dummy constructor for your class.
Dummy means method/constructor with do-nothing body. (From topic Object Class and Constructors in Java)
Ex
Sample(){}
A(){}
Student(){}
Hence these dummy constructors are responsible for allocating memory to our objects.
Constructors doesnot have written type i.e.do not use void also.
Ex
Constructor can take parameters.
In the body of the constuctor, we can initialize data members of a class, to the object which is created.
Ex
Student s1=new Student();
Student()
{
Roll no=1001;
Sname="sai";
Age=21;
}
constructors are also responsible for providing default values to the data members of a class when an object is created. Provided the class has dummy constructor. (From topic Object Class and Constructors in Java)
Student s2=new Student();
in the above statement JVM provides dummy constructor Student(){}.
Default values of constructors
- integer---0
- floating---0.0
- Character---'\u0000'
- boolean---false
- object---null
- String---null
//program related to Object Class and Constructors in Java--constructors
class ConstructorClass1
{
public static void main(String args[])
{
Exampleclass obj1=new Exampleclass();
System.out.println("object1 char: "+obj1.cc);
System.out.println("object1 char: "+obj1.ii);
System.out.println("object1 char: "+obj1.dd);
}
}
class Exampleclass
{
char cc;
int ii;
double dd;
Exampleclass() //Exampleclass(){} dummy
{
cc='a';
ii=10;
dd=100.4;
}
}
Output
To get the code file use me.
Continue to the next topic Constructors and notations in java.
Begin your career in Digital Marketing,What is digital marketing? Digital Marketing online course. It's an current evolving technology which can give support to establish your own startup through Digital Marketing.
Do check my new startup Surprise Planners in Hyderabad- Lavish Surprises our services are surprise party planners in Hyderabad, surprise gifts, surprise birthday party planners Hyderabad, Wedding anniversary surprises, surprise planners Hyderabad.
Hi Friends, Please comment down your views in the comment section below. Thank you...
No comments:
Post a Comment