Pages

Wednesday, 8 March 2023

Explain different types of variables in Java with examples?

 

SDET Interview Question and Answers.  

Java Interview Question SET-01

Java INterview Question Set-02 ARRAY

Java INterview Questiona Set-03 MAP

Java INterview Question Set-04


In Java, there are three types of variables: local variables, instance variables, and class/static variables.  Here's an explanation of each type with examples:

  1. Local Variables:

    These are variables that are declared inside a method, constructor, or block. They are only accessible within the scope of the method, constructor, or block in which they are declared. Local variables must be initialized before they can be used.

Example:


public class LocalVariablesExample {
    public static void main(String[] args) {
        int x = 5; // local variable
        System.out.println(x); // prints 5
    }
}


  1. Instance Variables:


    These are variables that are declared within a class, but outside of any method, constructor, or block. They are also known as non-static variables, as they are associated with an instance of a class. Instance variables can be accessed and modified using the instance of the class.

Example:


public class InstanceVariablesExample {
    int x; // instance variable

    public static void main(String[] args) {
        InstanceVariablesExample obj = new InstanceVariablesExample();
        obj.x = 5; // assigning a value to the instance variable
        System.out.println(obj.x); // prints 5
    }
}


  1. Class/Static Variables:

    These are variables that are declared within a class, but outside of any method, constructor, or block, and are marked as static. Class variables are associated with the class itself, rather than with an instance of the class. They can be accessed and modified using the class name.

Example:

public class StaticVariablesExample {
    static int x = 5; // class variable

    public static void main(String[] args) {
        System.out.println(StaticVariablesExample.x); // prints 5
    }
}


In this example, x is declared as a static variable within the StaticVariablesExample class. We can access and modify this variable using the class name, rather than an instance of the class. We can see this in the main method where we use StaticVariablesExample.x to access the value of x.


4. Reference Variable


In Java, a reference variable is a variable that stores the memory address of an object rather than the object itself. When you create an object in Java, it is stored in the heap memory and the reference to that object is stored in the stack memory.

Reference variables are used to access and manipulate objects in Java. You can think of them as pointers in other programming languages. Unlike primitive data types (such as int, float, double, etc.), which hold the actual data value, reference variables hold a reference to the memory location where the object is stored.

Here is an example of a reference variable in Java:


public class ReferenceVariableExample {
    public static void main(String[] args) {
        // Create a new String object
        String str = new String("Hello World");

        // Assign the reference of the String object to a reference variable
        String strRef = str;

        // Print the values of the original reference variable and the new reference variable
        System.out.println("Original Reference Variable: " + str);
        System.out.println("New Reference Variable: " + strRef);
    }
}

In this example, we create a new String object and assign its reference to the str reference variable. We then assign the same reference to a new reference variable strRef. Both reference variables point to the same object in memory. When we print the values of the two reference variables, we get the same output, which is the value of the String object: Hello World.




To calculate Test Estimation, check the link below:
Automation Test Estimation


What are the Types of APIs ?


Gmail Automation with Cypress & JavaScript


*******************************************************************
For any doubts or career guidance, reach out to me 


********************************************************************





Do remember that knowing Linux is one of the most important aspect to work as an SDET.

Basic Linux Commands for Automation QA


****************************************

TOP 15 BDD - CUCUMBER Interview Q&A


************************************************

✍️AUTHORLinkedIn Profile

************************************************

Learn (API-Microservice)Testing+ Selenium UI Automation-SDET with Self Paced Videos prepared by FAANG employees and LIVE Doubt Session 

SDET TRANING VIDEOS AVAILABLE with Live Doubt Session(course-1 below,API TRaining Videos With Class Notes and Coding Set) and (API+UI, both course-1 & 2 below) Check Training Page for Course Content or reach out @whatsapp +91-9619094122. 
This includes classnotes, 300+ interview questions, 3 projects, Java Coding question set for product companies along with career guidance from FAANG employees for Automation and SDET.

For more details whatsapp : https://lnkd.in/dnBWDM33

*************************************************

SeleniumWebdriver Automation Testing Interview Questions:

https://automationreinvented.blogspot.com/search/label/SeleniumWebdriver

API Testing Interview Question Set:

https://automationreinvented.blogspot.com/2022/03/top-80-api-testing-interview-questions.html

DevOps Interview Q&A:

https://automationreinvented.blogspot.com/2021/11/top-11-devops-interview-questions-and.html 

Kubernetes Interview Question Set

https://automationreinvented.blogspot.com/search/label/Kubernetes

Docker Interview Question Set

https://automationreinvented.blogspot.com/Docker

Linux Interview question Set

https://automationreinvented.blogspot.com/search/label/Linux

Automation Testing/SDET Framework Design

https://automationreinvented.blogspot.com/search/label/FrameworkDesign

Java Related Interview Question Set

https://automationreinvented.blogspot.com/search/label/Java

GIT Interview Question Set:

https://automationreinvented.blogspot.com/2021/09/top-40-git-interview-questions-and.html

Coding Interview Question Set:

https://automationreinvented.blogspot.com/search/label/Coding%20Questions

Mobile Testing Interview Question Set:

https://automationreinvented.blogspot.com/search/label/Mobile%20Testing

Python Interview Question Set for QAE - SDET - SDE:

https://automationreinvented.blogspot.com/search/label/Python

No comments:

Post a Comment