How to enter a non static method from a static one - java

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
}

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;
}
}

How can I use a Variable from a Class Runner to use in a Method in another Class? (BlueJ, Java)

I'm taking a coding class using BlueJ, and decided to surprise my teacher with a Text Adventure that uses a Class Runner and another class to have the methods to call. The problem is, I don't know how to use a variable that I established in the Runner, into a method in the Method Class, which I will then call into the Runner. Here is my code:
(This is the runner)
import java.util.*;
public class TextAdventureRunner
{
public static void main (String[]Args)
{
TextAdventureCode run = new TextAdventureCode();
Scanner kb = new Scanner(System.in);
String x = "";
System.out.print("Enter Your Name: : ");
x = kb.nextLine();
System.out.println(x);
run.Hi();
run.HiTwo();
}
}
(This is the code that contains the methods)
import java.util.*;
public class TextAdventureCode extends TextAdventureRunner
{
Scanner kb = new Scanner(System.in);
public static void Hi()
{
System.out.println("Hi" + x);
}
public static void HiTwo()
{
System.out.println("");
}
}
You see, In my method Hi(), there is an error where the x should be. the error reads "cannot find symbol - variable x" even though i extended the class and declared an object in the other class... any help?
You can declare your TextAdventureCode like this:
import java.util.*;
public class TextAdventureCode extends TextAdventureRunner
{
Scanner kb = new Scanner(System.in);
public static void Hi(String x) //Modification
{
System.out.println("Hi" + x);
}
public static void HiTwo()
{
System.out.println("");
}
}
And declare TextAdventureRunner like this:
import java.util.*;
public class TextAdventureRunner
{
public static void main (String[]Args)
{
TextAdventureCode run = new TextAdventureCode();
Scanner kb = new Scanner(System.in);
String x = "";
System.out.print("Enter Your Name: : ");
x = kb.nextLine();
System.out.println(x);
run.Hi(x); // Modification
run.HiTwo();
}
}

Java - call void function that print my name

public class Main {
public static void main(String[] args) {
**how i call the function here** ?
}//public
public static void nameAndAge() {
System.out.println("Name: Rubik Seviyants Age: 27");
}
}//main
package com.pervacio.adminportal.tradein.constants;
public class Main {
public static void main(String[] args) {
nameAndAge();
}//public
public static void nameAndAge() {
System.out.println("Name: Rubik Seviyants Age: 27");
}
}//main
Please read this for better understanding: Java Methods | Tutorials Point
If the function that has to be called is in the same class then just call the function like this:
function_name(parameters);
But if the function is in another class then you will have to create an object of the class and call:
Class_name object_name=new Class_name(); object_name.function_name();
These are the ways to call the function :
public class Main {
public static void main(String[] args) {
nameAndAge();
// or
Main.nameAndAge();
// or
Main s=new Main();
s.nameAndAge();
} //public
public static void nameAndAge() {
System.out.println("Name: Rubik Seviyants Age: 27");
}
}

Access to variables in same class but different methods in 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;
}
}

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
}

Categories

Resources