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
1,842 views

4 answers to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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,040 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,310 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,600 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,600 points
208 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
75 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
30 views
0 votes
1 answer

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

To add zeros to a number, we ...READ MORE

answered May 25, 2018 in Java by Akrati
• 3,150 points
118 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,360 points

edited Nov 6, 2018 by Omkar 5,364 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,050 points
1,418 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,050 points
3,885 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,050 points
273 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,840 points
28 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,040 points
161 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.