Skip to main content
Edited code to reflect tests
Source Link
B J
  • 140
  • 1
  • 3
  • 11

Your problem is that you are trying to find a local variable in a separate class. Variables can be set outside of a method like main(), and if they are public variables, they can be easily accessed by another class.

Outside of the main() function, inside the Main class, you would place the numberSearch variable. Then inside Finder you can access numberSearch by getting Main.numberSearch's value.

Be aware that if your professor wants you to use a private variable, you will need to use getters and setters.

/*How I tested the code*/ public class Main { public static int numberSearch; public static void main(String args[]) { int target = (int) (Math.random() * 1000); numberSearch = target;   /* some code */Finder.getResult(); } } /*in a separate file*/ public class Finder {     public static void getResult() { int someVariablet = Main.numberSearch; /* some code */ System.out.println(t); }  } 

Your problem is that you are trying to find a local variable in a separate class. Variables can be set outside of a method like main(), and if they are public variables, they can be easily accessed by another class.

Outside of the main() function, inside the Main class, you would place the numberSearch variable. Then inside Finder you can access numberSearch by getting Main.numberSearch's value.

Be aware that if your professor wants you to use a private variable, you will need to use getters and setters.

public class Main { public static int numberSearch; public static void main(String args[]) { int target = (int) (Math.random() * 1000); numberSearch = target;   /* some code */ } } public class Finder { int someVariable = Main.numberSearch; /* some code */ } 

Your problem is that you are trying to find a local variable in a separate class. Variables can be set outside of a method like main(), and if they are public variables, they can be easily accessed by another class.

Outside of the main() function, inside the Main class, you would place the numberSearch variable. Then inside Finder you can access numberSearch by getting Main.numberSearch's value.

Be aware that if your professor wants you to use a private variable, you will need to use getters and setters.

/*How I tested the code*/ public class Main { public static int numberSearch; public static void main(String args[]) { int target = (int) (Math.random() * 1000); numberSearch = target; Finder.getResult(); } } /*in a separate file*/ public class Finder {     public static void getResult() { int t = Main.numberSearch;  System.out.println(t); }  } 
added 11 characters in body
Source Link
B J
  • 140
  • 1
  • 3
  • 11

Your problem is that you are trying to find a local variable in a separate class. Variables can be set outside of a method like main(), and if they are public variables, they can be easily accessed by another class.

Outside of the main() function, inside the Main class, you would place the numberSearch variable. Then inside Finder you can access numberSearch by getting Main.numberSearch's value.

Be aware that if your professor wants you to use a private variable, you will need to use getters and setters.

public class Main { public static int numberSearch; public static void main(String args[]) { int target = (int) (Math.random() * 1000); numberSearch = target; /* some code */ } } public class Finder {   int someVariable = Main.numberSearch; /* some code */ } 

Your problem is that you are trying to find a local variable in a separate class. Variables can be set outside of a method like main(), and if they are public variables, they can be easily accessed by another class.

Outside of the main() function, inside the Main class, you would place the numberSearch variable. Then inside Finder you can access numberSearch by getting Main.numberSearch's value.

Be aware that if your professor wants you to use a private variable, you will need to use getters and setters.

public class Main { public int numberSearch; public static void main(String args[]) { int target = (int) (Math.random() * 1000); numberSearch = target; /* some code */ } } public class Finder { someVariable = Main.numberSearch; /* some code */ } 

Your problem is that you are trying to find a local variable in a separate class. Variables can be set outside of a method like main(), and if they are public variables, they can be easily accessed by another class.

Outside of the main() function, inside the Main class, you would place the numberSearch variable. Then inside Finder you can access numberSearch by getting Main.numberSearch's value.

Be aware that if your professor wants you to use a private variable, you will need to use getters and setters.

public class Main { public static int numberSearch; public static void main(String args[]) { int target = (int) (Math.random() * 1000); numberSearch = target; /* some code */ } } public class Finder {   int someVariable = Main.numberSearch; /* some code */ } 
added 150 characters in body
Source Link
B J
  • 140
  • 1
  • 3
  • 11

Your problem is that you are trying to find a local variable in a separate class. Like classes, variablesVariables can be declaredset outside of a method like main(), and if they are public variables, they can be easily accessed by another class or function.  

Outside of the main(So) function, above or belowinside the Main class, you can create a public intwould place the numberSearch variable.) Then inside Finder you can access numberSearch by getting Main.numberSearch's value.

So, in practice,Be aware that if your professor wants you to find theuse a private variable, you would havewill need to do something like so:use getters and setters.

public int numberSearch; public class Main { public int numberSearch; public static void main(String args[]) { int target = (int) (Math.random() * 1000); numberSearch = target; /* some code */ } } public class Finder {   someVariable = Main.numberSearch;  /* some code */ } 

After you set numberSearch in the main() function, you can use the stored variable in the Finder class.

Your problem is that you are trying to find a local variable in a separate class. Like classes, variables can be declared outside of a class or function.  (So, above or below the Main class, you can create a public int variable.)

So, in practice, to find the variable, you would have to do something like so:

public int numberSearch; public class Main { public static void main(String args[]) { int target = (int) (Math.random() * 1000); numberSearch = target; /* some code */ } } public class Finder { /* some code */ } 

After you set numberSearch in the main() function, you can use the stored variable in the Finder class.

Your problem is that you are trying to find a local variable in a separate class. Variables can be set outside of a method like main(), and if they are public variables, they can be easily accessed by another class.

Outside of the main() function, inside the Main class, you would place the numberSearch variable. Then inside Finder you can access numberSearch by getting Main.numberSearch's value.

Be aware that if your professor wants you to use a private variable, you will need to use getters and setters.

public class Main { public int numberSearch; public static void main(String args[]) { int target = (int) (Math.random() * 1000); numberSearch = target; /* some code */ } } public class Finder {   someVariable = Main.numberSearch;  /* some code */ } 
Source Link
B J
  • 140
  • 1
  • 3
  • 11
Loading