Does Java have global variables?

I need help with Java variables?

  • Lets say I have two java files: class Hello and class GoodBye Class Hello only contains one int variable and its value. int number = 5; Class GoodBye contains two int variables and their values. int number; int secondnumber; Suppose secondnumber has a value assigned, but number does not. I want the variable "number" in class GoodBye to get its value from the value of variable "number" from class "Hello"(which is 5). How do I do this? Both the programs are in the same directory on my computer.

  • Answer:

    I didn't properly get what you asked because the additional details and the main questions look different.I think the following code is according to what you asked: /*save the Hello class in Hello.java.Compile it.But don't run it.*/ public class Hello { int number=5; public int getNum() { return number; } } import java.util.*; class GoodBye { public static void main(String [] args) { GoodBye g=new GoodBye(); g.go(); } public void go() { int number; int secondNumber; Scanner myScanner = new Scanner(System.in); System.out.print("What is the number?"); secondNumber = myScanner.nextInt(); Hello h=new Hello(); number=h.getNum(); System.out.println("number="+number); System.out.println("secondNumber="+se… } }

Matthew at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

It sounds like these are two totally separate programs. If this is the case, you would have to write the variable to a file of some sort then read it in the other program. On the other hand, if these two classes are supposed to be part of the same program, you need to change a few things. You would need to take the main method out of one of the classes (probably Number) and instead replace it with constructors (probably Number()). Then, to get the value of "number" from the second class (the one with constructors now) you would simply create a new instance of that object (Number n = new Number()) and then you can do n.getNumber() if you have getters written or just n.number if number is public. Perhaps if you explained exactly what you are trying to do (why would you need two programs to do this?) it would be easier to help with your specific situation. Feel free to e-mail me this information and I'll try to help if you would like.

first of all u need a main class that will operate the classes (i don't know if u have already added this, but u don't give more informations ) moreover, both classes must be on private or else the compiler will have problems reading 2 variables with the same name and differentnt values, if u dont set the classes this way, after the class hello execute the variety number in GoodBye would be already 5, so this is a way actually to do this ;P would be better if u could add the full code of this

public class Number { // the class's constructor public Number (int n) { this.number = n ; } //HERE IS a CHANGE // this is a filed for the class ur creating private int number = 0 ; public getNumber( ) { return this.number; } public static void main(String args[]) { } } import java.util.Scanner; public class EnterNumber { public static void main(String args[]) { //create an object of class Number and give it the value of number u //wanna int aNm Number x = new Number( aNumber); Scanner myScanner = new Scanner(System.in); int number; int numberentered; System.out.print("What is the number?") numberentered = myScanner.nextInt(); if (numberentered == number) { System.out.print("That is right"); } else { System.out.print("Wrong"); } } } Test the code !

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.