how to do global variable declaration in java

0 votes
Apr 3 in Java by anonymous
• 120 points
42 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 by Niroj
        • 23,950 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
        • 53,110 points
        390 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,890 points
        1,296 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,090 points

        edited Dec 19, 2018 by Daisy 96 views
        +5 votes
        3 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,420 points

        edited Nov 6, 2018 by Omkar 22,181 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,090 points
        227 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,190 points
        130 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,090 points
        117 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
        • 53,110 points
        152 views
        0 votes
        1 answer

        How to convert InputStream to byte array in Java?

        Hello @kartik, You need to read each byte ...READ MORE

        answered Apr 9 in Java by Niroj
        • 23,950 points
        589 views
        0 votes
        1 answer

        How to set AUTO-SCROLLING of JTextArea in Java GUI?

        Hii kartik, When you click anywhere over JTextArea, ...READ MORE

        answered Apr 21 in Java by Niroj
        • 23,950 points
        24 views