class. A class can have more than one constructor and depending
on the parameters given
and return type expected, the matching constructor is called. A sample implementation for
this type of a method is given below:
public class Puppies{
public Puppies(){
}
public Puppies(string puppyname){
}
The above class has two constructors. One of the constructors requires no parameters.
However, the other constructor requires a string equivalent to the name of the puppy. Java
additionally upholds Singleton Classes where you would have the
capacity to make one
and only object of a class.
Making Objects
As specified previously, a class gives the outlines to object creation. So, fundamentally an
object is made from a class.
In Java, the new essential word is utilized to make new
objects.
There are three steps involved in the creation of any object. These steps are illustrated
below:
Declaration: A variable assertion with a variable name and object type.
Instantiation: The “new” word is utilized to make the object
of an already declared
class.
Initialization: The “new” word is trailed by a call to a constructor. This call
instantiates the class and creates an object of the same, as a result.
Sample implementation is given below for better understanding of the concept.
public class Puppies{
public Puppies(string name){
System.out.println(“Passed Name of the puppy is:” + name );
}
public static void main(string []args){
Puppies samplepuppy = new Puppies( “jimmy” );
}
On the off chance that we compile
and run the above project, then it would deliver the
accompanying result:
Passed Name of the puppy is: jimmy
Do'stlaringiz bilan baham: