Details of Object, Variable Types and Constructor in Java - Selenium Tutorials For Beginners

Java is one of the prominent programming languages and is being used widely across the world. In this part of the java basics, we will discuss Object, Variable Types and Constructor.

Objects in Java

An object in java is an instance of a class. In other words, it is an entity which has a state and behavior. An object in java has three characteristics

  • State representing the data or the value of the object
  • Behaviour representing the functionality of the object including deposit and withdrawal
  • An identity which is implemented by a unique ID which is not visible to the external user.

Variables in Java

A piece of memory which contains a data value is called as a variable in Java. Or it can be called as the name of a memory location. Basically, there are three different types of variables in Java.

  • Local Variable: – when the variable is declared inside the method, it is called as a local variable.
  • Instance Variable: – When the variable is declared outside the method but inside the class, it is called as Instance Variable.
  • Static Variable: – When the variable is declared as static, it is called as a static variable. It can never be a local variable.

Examples of variables in Java

  1. class A{  
  2. int data=50;//instance variable  
  3. static int m=100;//static variable  
  4. void method(){  
  5. int n=90;//local variable  
  6. }  
  7. }//end of class  

Constructor in Java

The special type of method used for initializing an object is called as a constructor in Java. It has to be invoked at the time of object construction itself. It provides the data (Constructs Values) for the project and hence is called a constructor.

You should take care of two things or follow two rules while creating the constructor. First one is that you should give the same name to the constructor as that of its class and the second rule is that the constructor should not have any explicit return type.

There are two different types of constructors in Java and they are

  • Default constructor, which is also known as a no-arg constructor.
  • Parameterized constructor

When the constructor does not have any parameters it is called as default constructor.

Example for Default constructor in Java

  1. class Bike1{  
  2. Bike1(){System.out.println(“Bike is created”);}  
  3. public static void main(String args[]){  
  4. Bike1 b=new Bike1();  
  5. }  
  6. }  

Default constructor provides default values such as zero, null etc to the objects.

At the same time when the constructor has well-defined parameters, it is called as Parameterized constructor

Example for Parameterized Constructor in Java

  1. class Student4{  
  2.  int id;  
  3. String name;  
  4. Student4(int i,String n){  
  5. id = i;  
  6. name = n;  
  7. }  
  8. void display(){System.out.println(id+” “+name);}  
  9. public static void main(String args[]){  
  10. Student4 s1 = new Student4(111,”Karan”);  
  11. Student4 s2 = new Student4(222,”Aryan”);  
  12. s1.display();  
  13. s2.display();  
  14. }
  15. }  

Learning Objects, variables, and constructors in Java is an important part of java learning and the same will be dealt in detail by the trainers.

Leave a Reply

Your email address will not be published. Required fields are marked *