Access to variables in same class but different methods in java - java

i'm new with java and i have some a difficult to access some variables from different methods but in the same class.
my code:
class upgradeSim {
public static void main(String[] args) throws JSchException {
System.out.print("\n[INFO]: Please enter the version you would like to upgarde to: ");
Scanner inputVer = new Scanner(System.in);
String uiVersion = inputVer.nextLine();
}
public static void SendShhCmd() {
//some code
}
public static void StartUpgrade() throws JSchException {
String cmd = ("SCP data/mdusr/perforce/automationtools/builds/ui/"+uiVersion);
}
}
the problem is that StartUpgrade method does not recognized uiVersion variable.
i tired using "this" and "super" but no luck...
thanks for the help.

Declare the uiVersion as a static member of the upgradeSim class:
class upgradeSim {
public static String uiVersion;
public static void main(String[] args) throws JSchException {
System.out.print("\n[INFO]: Please enter the version you would like to upgarde to: ");
Scanner inputVer = new Scanner(System.in);
uiVersion = inputVer.nextLine();
}
...
public static void StartUpgrade() throws JSchException {
// Now, 'uiVersion' is accessible
String cmd = ("SCP data/mdusr/perforce/automationtools/builds/ui/"+uiVersion);
}
}

Make uiVersion a class variable:
class upgradeSim {
public static String uiVersion;
etc.
}
And set the variable in your main method like this:
uiVersion = inputVer.nextLine();

You should create a global variable named uiVersion like this:
class upgradeSim {
static String uiVersion ;
public static void main(String[] args) throws JSchException {
...
uiVersion = inputVer.nextLine();
...

uiVersion varibale is local, only accessible in main method, so you cannot access to it from StartUpgrade method.
Declare the uiVersion as a static member of the UpgradeSim class
class UpgradeSim {
private static String UI_VERSION;
public static void main(String[] args) throws JSchException {
System.out.print("\n[INFO]: Please enter the version you would like to upgarde to: ");
Scanner inputVer = new Scanner(System.in);
UI_VERSION = inputVer.nextLine();
}
public static void SendShhCmd() {
//some code
}
public static void StartUpgrade() throws JSchException {
String cmd = ("SCP data/mdusr/perforce/automationtools/builds/ui/"+uiVersion);
// use UI_VERSION;
}
}

Related

Is there a way to take a string from inside a public static void and move it "outside" into a new class into a public static string?

In a small example, I'm trying to take information from the String "helloFromMain" in a file called Main.java and move it "outside" the public static void into a public static string in a different file named data.java.
IN THE FILE MAIN.JAVA
public class Main {
public static void main(String[] args){
String helloFromMain = "hello";
}
}
IN THE FILE DATA.JAVA
public class Data {
public static String helloFromData = helloFromMain;
}
Any help would be appreciated.
Also im relatively new to all this
You can set a public static variable from another class.
public class Main {
public static void main(String[] args){
String helloFromMain = "hello";
Data.helloFromData = helloFromMain;
}
}
Also, I've found that it's helpful to set a package for all classes, as it makes it simpler to manage importing and FS structure.
You can do it like this
Simple way.
IN THE FILE MAIN.JAVA
public class Main {
public static String helloFromMain; //declare a public static string, it will be accessible from outside the class.
public static void main(String[] args){
helloFromMain = "hello";
}
}
IN THE FILE DATA.JAVA
public class Data {
public static String helloFromData = Main.helloFromMain;
}
But be aware that Main.helloFromMain will be null until you call the main constructor.
Advance way :
IN THE FILE MAIN.JAVA
public class Main {
private static String helloFromMain; //This time the static variable is private, so you can't directly use it from outside
public static void main(String[] args){
helloFromMain = "hello";
}
//We create a public static method to access the private static variable
public static String getHelloFromMain(){
return helloFromMain;
}
}
IN THE FILE DATA.JAVA
public class Data {
//We call our public static method from Main.
public static String helloFromData = Main.getHelloFromMain();
}
You can create a static method in class Data to set the value of the string. If you do this, you don't need to make the variable in class Data public.
public class Main {
public static void main(String[] args) {
String helloFromMain = "hello";
Data.setHelloFromData(helloFromMain);
}
}
public class Data {
private static String helloFromData;
public static void setHelloFromData(String newValue) {
helloFromData = newValue;
}
}

Is it possible to get terminal input in init()?

I have this code:
public class test {
init() {
//How can i get access of args?
}
public static void main(String[] args) {
new test();
}
How can I get access of "args" in init()?
Just pass it as an argument.
public class test{
init(String[] args){
//How can i get access of args?
}
public static void main(String[] args) {
test t = new test();
t.init(args);
}
}
Remember, you should capitalise your class names.
Define an instance field within the class, and create constructor to accept the array and set the field:
public class test{
String[] args;
public test(String[] args){
this.args = args;
}
init(){
}
}
public static void main(String[] args) {
new test(args);
}
Or pass it as an argument to the method.
init(String[] args){
}
If init requires anything within the test class, you must create an instance of the class
test t = new test();
t.init(args);
If init does not require a new instance of test (eg doesn't access any instance variables or methods) you could define init as static and directly call the method:
static init(String[] args){//method declaration
}
public static void main(String[] args) {
test.init(args);//call the method in a static way
}

Declaration for an array being called into main from a method (Java)

I am fairly new to java, and am having what I assume is a simple problem with my program.
For the method arrayTest2, I cannot import it into main due to an error on compilation:
"Cannot find symbol, symbol: variable dataStorage".
I have tried also tried the declarations:
arrayTest2(dataStorage[][])
and
arrayTest2(dataStorage[5][5])`
but they don't work.
Any help would be greatly appreciated.
Cheers
import io.*;
public class TrialArray
{
public static void main(String [] args)
{
arrayTest();
arrayTest2(dataStorage);
}
public static void arrayTest()
{
int[][] dataStorage = new int[5][5];
dataStorage[1][2] = 1;
System.out.printf("THIS PART WORKS");
}
public static void arrayTest2(int[][] dataStorage)
{
dataStorage[2][2] = 3;
System.out.printf("THIS DOESNT");
}
}
The problem here is the scope: Something defined in one function is not visible in another. What you will normally do to solve this is to return the value. Something like this:
import io.*;
public class TrialArray
{
public static void main(String [] args)
{
int[][] dataStorage = arrayTest();
arrayTest2(dataStorage);
}
public static int[][] arrayTest()
{
int[][] dataStorage = new int[5][5];
dataStorage[1][2] = 1;
System.out.printf("THIS PART WORKS");
return dataStorage;
}
public static void arrayTest2(int[][] dataStorage)
{
dataStorage[2][2] = 3;
System.out.printf("THIS DOESNT");
}
}
Alternatively you could have your dataStorage field as a global variable, this is however potentially very confusing. To do that you'd define
public class TrialArray
{
private static int[][] dataStorage;
// ...
public static void arrayTest() {
dataStorage = new int[5][5];
dataStorage[1][2] = 1;
System.out.printf("THIS PART WORKS");
}
// ...
}
on this line
arrayTest2(dataStorage);
You are passing parameter to method, that has one argument "dataStorage", but you don't declare it.
You try to pass dataStorage to your arrayTest-function, but dataStorage is not a field of the class, neither is it a local variable of main (aka dataStorage does not exist in main).
public static void main(String [] args) {
arrayTest();
arrayTest2(dataStorage); //<------- What is dataStorage?
}
Here is a little tutorial on variable scopes in Java. You probably want to return the array you created in arrayTest() and use it, but I am just guessing what you want to do.
You cant access variables in declared inside other methods.
To make it work, you would have to do this:
public class TrialArray
{
int[][] dataStorage;
public static void main(String [] args)
{
dataStorage = new int[5][5];
arrayTest();
arrayTest2(dataStorage);
}
public static void arrayTest()
{
dataStorage[1][2] = 1;
System.out.printf("THIS PART WORKS");
}
public static void arrayTest2(int[][] dataStorage)
{
dataStorage[2][2] = 3;
System.out.printf("THIS DOESNT");
}
}

How to enter a non static method from a static one

CollegeTester.java:10: error: non-static method getCommand() cannot be referenced from a static context
getCommand();//goes to command
^
How would I enter this method. Making it public static void only causes more problems
import java.util.Scanner;
public class CollegeTester
{
public String name;
Scanner input = new Scanner(System.in);
public static void main(String[] args)
{
getCommand();//goes to command
}
//Ask user for a command
public void getCommand()
{
// do stuff
}
}
You can call it as in main :
CollegeTester c = new CollegeTester();
c.getCommand();
Non-static methods are instance methods, and thus accessed over an instance of the class:
new CollegeTest().getCommand();
Create an instance of the CollegeTester class in main method and call the method.
new CollegeTester().getCommand();
You need to create an instance of CollegeTester :
main(...)
{
CollegeTester t = new CollegeTester();
t.getCommand();
}
getCommand() is not static so you can not call this in main() as main() is static.You have create an object and then call getCommand().
Or make getCommand() static
This is the way by creating object and calling getCommand()
import java.util.Scanner;
public class CollegeTester
{
public String name;
Scanner input = new Scanner(System.in);
public static void main(String[] args)
{
CollegeTester c=new CollegeTester();
c.getCommand();//goes to command
}
//Ask user for a command
public void getCommand()
{
do stuff
}
This is the way of making getCommand() static
import java.util.Scanner;
public class CollegeTester
{
public String name;
Scanner input = new Scanner(System.in);
public static void main(String[] args)
{
getCommand();//goes to command
}
//Ask user for a command
public static void getCommand()
{
do stuff
}

Variable Accessible by All Methods Without Parameters?

Is it possible to have a "global" variable, i.e., "balance", which all methods can access without parameters?
Something like:
public static void main(String[] args{
makevariablehere
}
Could be called in another method:
public static int someMethod() {
variable = newVariable;
}
You can define it as a static field on the class. See the example below, which stores the number of args passed to the main method in a static field, so that it may be returned by the getNumberOfArgs() method.
public class MyClass {
private static int argCount;
public static void main(String[] args) {
argCount = args.length;
}
public static int getNumberOfArgs() {
return argCount;
}
}

Categories

Resources