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
118,014 views

6 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,140 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,370 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 Parth
• 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, 2019 by zemiak
0 votes

Global variables are not technically allowed in Java. A global variable is one declared at the start of the code and is accessible to all parts of the program. Since Java is object-oriented, everything is part of a class. ... A static variable can be declared, which can be available to all instances of a class.

To know more about Java, without wasting anytime go for Java certification course now.

answered Dec 15, 2020 by Roshni
• 10,480 points
0 votes

To define Global Variable you can make use of static Keyword

public class Example {
    public static int a;
    public static int b;
}

now you can access a and b from anywhere by calling

Example.a;

Example.b;
answered Dec 15, 2020 by Gitika
• 65,770 points

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 Parth
• 4,640 points
3,028 views
0 votes
2 answers

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

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

answered May 24, 2018 in Java by UshaK
2,772 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,190 points
1,260 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, 2019 in Java by Sirajul
• 59,230 points
5,188 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,394 views
+1 vote
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,500 points
8,409 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,500 points
17,511 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,500 points

edited Aug 4, 2023 by Khan Sarfaraz 4,915 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,920 points
1,436 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,140 points
1,812 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