Java-Programm für den Zugriff auf (System wirft Syntaxfehler auf Token „int“ Import in der globalen Deklaration erwartet „int puppyAge;“: -Wie für den Zugriff auf Instanzvariablen und Methoden Instanzvariablen und Methoden in Java
Ist es eine Option, um die Variable global zu deklarieren: -.
package instancevariableandmethods;
int puppyAge;
public class Instancevariable {
public Instancevariable(String name) {
System.out.println("The name is :"+name);
}
public void setAge(int age) {
puppyAge = age;
}
public int getAge() {
System.out.println("Puppy's age is :" + puppyAge);
return puppyAge;
}
public static void main(String[] args) {
Instancevariable myPuppy = new Instancevariable("tommy");
/* Call class method to set puppy's age */
myPuppy.setAge(2);
/* Call another class method to get puppy's age */
myPuppy.getAge();
/* You can access instance variable as follows as well */
System.out.println("Variable Value :" + myPuppy.puppyAge);
}
}
hängt davon ab, was Sie mit "global" meinen. Java hat keine globalen Variablen, alles ist innerhalb einer Klasse – Stultuske