How can we define global variables in java?

0 votes
I want to call the variables from anywhere, for which I need to declare them as global. How to do that?
Apr 19, 2018 in Java by sophia
• 1,400 points
4,616 views

4 answers to this question.

0 votes

To make the variable as global , you can use the keyword static:

public class Global
{
    public static int x;
    public static int y;
}
answered Apr 19, 2018 by Daisy
• 8,050 points
0 votes
You could create a set of public static members in a class named Globals.
public class Globals {
   public static int globalInt = 0;
   ///
}
answered Jul 26, 2018 by sharth
• 3,350 points
0 votes

Another way is to create an interface like this:

public interface GlobalConstants
{
  String name = "Chilly Billy";
  String address = "10 Chicken head Lane";
}

Any class that needs to use them only has to implement the interface:

public class GlobalImpl implements GlobalConstants
{
  public GlobalImpl()
  {
     System.out.println(name);
  }
}
answered Aug 24, 2018 by parth295
• 4,640 points
0 votes
class G {
  static String globalVar = "Global Value";
}

class Class1 {
  Class1() {
    System.out.println("Class1: "+G.globalVar);
    G.globalVar += " - changed";
} }
class Class2 {
  Class2() {
    System.out.println("Class2: "+G.globalVar);
} }

class Main {  
  public static void main(String[] args) {  
    Class1 o1 = new Class1();
    Class2 o2 = new Class2();
} }

/*
Output:
Class1: Global Value
Class2: Global Value - changed
*/
answered Jan 29 by zemiak

Related Questions In Java

0 votes
1 answer

How can we get file extension in Java?

To get the file extension, we can ...READ MORE

answered May 8, 2018 in Java by parth295
• 4,640 points
347 views
0 votes
2 answers

How can we remove an element from an array in Java?

You can use ArrayUtils class remove method which ...READ MORE

answered May 24, 2018 in Java by UshaK
120 views
0 votes
1 answer

How can we resolve ClassNotFoundException in Java?

Your classpath is broken. Depending on how you ...READ MORE

answered May 22, 2018 in Java by Akrati
• 3,150 points
69 views
0 votes
2 answers

How can we add leading zeros to the number in Java?

From Java 1.5 you can use the String.format method. ...READ MORE

answered Aug 26 in Java by Sirajul
• 30,940 points
317 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,380 points

edited Nov 6, 2018 by Omkar 9,258 views
0 votes
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,090 points
2,173 views
0 votes
1 answer

What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

See, both are used to retrieve something ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,090 points
5,647 views
0 votes
1 answer

Selenium JARS(Java) missing from downloadable link

Nothing to worry about here. In the ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,090 points
622 views
0 votes
4 answers

How can we compare dates in java?

public static String daysBetween(String day1, String day2) ...READ MORE

answered Sep 5, 2018 in Java by Sushmita
• 6,880 points
43 views
0 votes
3 answers

How can we use java.String.format in Scala?

String aString = "world"; int aInt = 20; String.format("Hello, ...READ MORE

answered Aug 29, 2018 in Java by Daisy
• 8,050 points
226 views