简体   繁体   中英

Can I use class variable in class method in Java?

I'm learning Java and practicing by myself. I tried to make a class variable String username and I thought I would get the result "Hi Nat" since method engHi() is in the same class with the variable.

class Greetings{
    String userName = "Nat";
    public static void engHi(String userName){
        System.out.println("Hi "+userName);
    }
}
public class Main {
    public static void main(String[] args) {
        Greetings.engHi();
    }
}

And I got this message.

Error:(11, 18) java: method engHi in class com.company.Greetings cannot be applied to given types;
  required: java.lang.String
  found: no arguments
  reason: actual and formal argument lists differ in length

and then I added this.username = username in the method engHi()

class Greetings{
    String userName = "Nat";
    public static void engHi(String userName){
        this.userName = userName;
        System.out.println("Hi "+userName);
    }
}
public class Main {
    public static void main(String[] args) {
        Greetings.engHi();
    }
}

And the result is

Error:(12, 18) java: method engHi in class com.company.Greetings cannot be applied to given types;
  required: java.lang.String
  found: no arguments
  reason: actual and formal argument lists differ in length

I thought since the method is in the same class I thought the method was gonna take the variable username 'automatically' so I didn't put any parameter when I called the method. So.. It means methods don't take any variable from same class?

The issue with your code here is that your method engHi() requires the a username string to be sent in as a parameter. If you remove the parameters of engHi() , this error should go away.

class Greetings{
    String userName = "Nat";
    public static void engHi(){ //nothing required to be passed in when you call engHi()
        System.out.println("Hi "+userName);
    }
}
public class Main {
    public static void main(String[] args) {
        Greetings.engHi();
    }
}

Either add a userame in call like Greetings.engHi("Nat"); or remove that from acceptig function like

public static void engHi(){
}

Your methof engHi is expecting a username which you're not passing during the call from Greetings.engHi();

The error has little to do with using class variables.

Rather, if a method is declared with N arguments, it needs to be called with N arguments.

Declaration: engHi(String userName) - 1 argument.

Call: Greetings.engHi() - 0 arguments.

The mismatch in argument count is a language violation.

Moreover, in these two lines:

String userName = "Nat";
public static void engHi(String userName){

the two occurences of username are not referring to the same thing. The first declares a member of an instance of the class; the second is a formal argument of the engHi method, which will be set to whatever value is provided as an actual argument by the caller.

A static method may only access static variables. A non- static method may access static and non- static variables.

Create an object of the Greetings class in engHi() method.

 public static void engHI() { 
   Greetings greetings = new Greetings(); 
   System.out.println("Hi " + greetings.userName);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM