how to do global variable declaration in java

0 votes
Apr 3, 2020 in Java by anonymous
• 120 points
719 views

1 answer to this question.

0 votes

Hello,​

Global varriable which is defined after the class tag and before Method. so there are two type of Global variable

1.Instace global variable

public class GolbalVariable{
public String username;
public String password;
}

    Explanation : this is called as instance variable . so on instance method you can access directly . suppose if you want to access inside statioc method then create Object of class and access it . if method is static then code to access the variable,

    ClassObject obj=new ClassObject();//create class Object from static method
    username;//if username is static
    obj.username; // if username is instance

    2.static global variable.

    public class GolbalVariable{
    public static String username;
    public static String password;
    }

    Explanation : You can access directly with class Name

    ClassObject obj=new ClassObject();//create class Object from static method
    obj.username;//if username is static

    ​Hope this is helpful!

    Thank You!!

        answered Apr 3, 2020 by Niroj
        • 82,840 points

        Related Questions In Java

        0 votes
        2 answers

        How can I convert a String variable to a primitive int in Java

         Here are two ways illustrating this: Integer x ...READ MORE

        answered Aug 20, 2019 in Java by Sirajul
        • 59,230 points
        2,310 views
        0 votes
        2 answers

        How do we convert JSON to Map in Java

        When you don't know structure of json. ...READ MORE

        answered Oct 31, 2018 in Java by Sushmita
        • 6,920 points
        6,013 views
        +2 votes
        1 answer

        How do I ask the Selenium-WebDriver to wait for few seconds in Java?

        There are two ways to wait: explicit ...READ MORE

        answered Dec 19, 2018 in Java by Daisy
        • 8,140 points

        edited Dec 19, 2018 by Daisy 950 views
        +5 votes
        4 answers

        How to execute a python file with few arguments in java?

        You can use Java Runtime.exec() to run python script, ...READ MORE

        answered Mar 27, 2018 in Java by DragonLord999
        • 8,450 points

        edited Nov 7, 2018 by Omkar 81,222 views
        0 votes
        5 answers

        How to compare Strings in Java?

        String fooString1 = new String("foo"); String fooString2 = ...READ MORE

        answered Jul 12, 2018 in Java by Daisy
        • 8,140 points
        2,548 views
        +1 vote
        2 answers

        How to generate random integers within specific range in Java?

        You can achieve that concisely in Java: Random ...READ MORE

        answered Jul 25, 2018 in Java by samarth295
        • 2,220 points
        1,253 views
        0 votes
        2 answers

        How to create a 2-D array in java?

        int[][] multi = new int[5][]; multi[0] = new ...READ MORE

        answered Jul 16, 2018 in Java by Daisy
        • 8,140 points
        1,543 views
        0 votes
        2 answers

        How to round any number to n decimal places in Java?

        new BigDecimal(String.valueOf(double)).setScale(yourScale, BigDecimal.ROUND_HALF_UP); will get you a BigDecimal. To ...READ MORE

        answered Aug 26, 2019 in Java by Sirajul
        • 59,230 points
        1,690 views
        0 votes
        2 answers

        How do I convert a String to an int in Java?

        Use the lines of code mentioned below:- String ...READ MORE

        answered Feb 9, 2022 in Java by Soham
        • 9,710 points
        3,208 views
        0 votes
        2 answers

        How to convert InputStream to byte array in Java?

        The class IOUtils, lib: https://mvnrepository.com/artifact/com ...READ MORE

        answered Sep 24, 2020 in Java by Anthony
        15,600 views
        webinar REGISTER FOR FREE WEBINAR X
        REGISTER NOW
        webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP