Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
The Person.setPhoneNumber is saying
Main method is not static in class PersonTest, please define the main method as:
public static void main(String[] args)
but I cannot define in Person class the string PersonName as static. What should I do then?
Person Class:
public class Person {
private String name;
private int age;
private String phoneNumber;
public Person(String n, int a){
name = n;
age = a;
phoneNumber = null;
}
public String getName(){
return name;
}
public void setname(String n){
this.name = n;
}
public int getAge(){
return age;
}
public void setAge(int a){
age = a;
}
public String getPhoneNumber(){
return phoneNumber;
}
public void setPhoneNumber(String pn){
this.phoneNumber = pn;
}
public String toString() {
return "Person {name=" + name +", age= " + age +", phone number =" + phoneNumber+ "}";
}
}
Person Test:
public class PersonTest {
public void main(String[] args){
Person person1 = new Person("Joel.Z", 20);
Person.setPhoneNumber("8324193601");
Person person2 = new Person("Fred Werd", 84);
Person.setPhoneNumber("585275333");
System.out.println(person1);
System.out.println(person2);
}
}
The main method should be static:
public static void main(String[] args)
Also, you should call you methods on an instance, not on a class:
public class PersonTest {
public static void main(String[] args){
Person person1 = new Person("Joel.Z", 20);
person1.setPhoneNumber("8324193601");
Person person2 = new Person("Fred Werd", 84);
person2.setPhoneNumber("585275333");
System.out.println(person1);
System.out.println(person2);
}
}
As #ΔλЛ says is correct, your main method must be static, also keep in mind that you must use the instance of the created object, it means:
Here you are creating person1 object:
Person person1 = new Person("Joel.Z", 20);
So, you must use that object to access the attribute:
person1.setPhoneNumber("8324193601");
instead of Person.setPhoneNumber("8324193601"); In the same way with the second object: person2.setPhoneNumber("585275333"); instead of Person.setPhoneNumber("585275333");
Related
This question already has answers here:
Modifier static is only allowed in constant variable declarations
(2 answers)
Closed 2 years ago.
My task is : Call the ‘setGrade’method to assign to the grade variable of S1 the value of 11
the code is:
public class student {
private String name;
private int age;
private int grade;
private double average;
private boolean disability;
private void printStudentInfo(){
//Data Encapsulation is methods of the public interface provide access to private data, while hiding implementation.
System.out.println("Name: "+name+",Age: "+age+",Grade: "+grade+",Average: "+average +" Disability: "+disability);
}
public void setGrade(int newGrade){
grade=newGrade;
}
public int getGrade(){
return grade;
}
//this part was given from the question
//from here
public class StudentTester{
public static void main(String[] args){
student S1 = new student();
student S2 = new student();
//to here
S1.setGrade(12);
}
}
}
Every time I run my program it gives me an error
modifier 'static' is only allowed in constant variable declarations
The tester class should contain the main method along with other static classes and methods.
public class StudentTester{
public static void main(String[] args){
student S1 = new student();
student S2 = new student();
S1.setGrade(12);
}
public static class student {
private String name;
private int age;
private int grade;
private double average;
private boolean disability;
private void printStudentInfo(){
//Data Encapsulation is methods of the public interface provide access to private data, while hiding implementation.
System.out.println("Name: "+name+",Age: "+age+",Grade: "+grade+",Average: "+average +" Disability: "+disability);
}
public void setGrade(int newGrade){
grade=newGrade;
}
public int getGrade(){
return grade;
}
}
}
Also check, Static Classes In Java
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
The code is having some problem with class 2 line 12. Kindly help .
The code is in the image.
import java.util.Scanner;
public class first{
public static void main(String[] args){
Scanner b = new Scanner(System.in);
pro nc = new pro();
System.out.println("Enter the name of your first true crush");
String temp = b.nextLine();
nc.setname(temp);
nc.etc();
}
}
public class pro{
private String gname;
public String getName() {
return gname;
}
public void setName(String name) {
this.gname = name;
}
public void etc(){
System.out.printf("The name of your true crush was %s",getName());
}
}
In class 2 you have not specified the location of the function in System.out.printf(). You should use the this keyword and re-write the function as
public class pro {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void etc(){
System.out.printf("The carp is %s",this.getName());
}
}
there is no problem with this
I AM USING NETBEANS IDE.
/*
///
public class pro {
private String gname;
public String getname() {
return gname;
}
public void setname(String name) {
gname = name;
}
public void etc(){
System.out.printf("Tht name is %s",getname());
}
}
/////////
import java.util.Scanner;
public class first {
public static void main(String[] args) {
Scanner b= new Scanner(System.in);
pro nc = new pro();
System.out.println("enter the name");
String temp = b.nextLine();
nc.setname(temp);
nc.etc();
}
}
/////////
*/
Recheck this.
I have not changed your code, just typed it.
And yes I agree with Arc676 that people should post the code.
There is no problem on netbeans.
You can try to use System.out.format instead of System.out.printf.
import java.util.Scanner;
public class first{
public static void main(String[] args){
Scanner b = new Scanner(System.in);
pro nc = new pro();
System.out.println("Enter the name of your first true crush");
String temp = b.nextLine();
nc.setname(temp);
nc.etc();
}
}
public static class pro{
private String gname;
public String getName() {
return gname;
}
public void setName(String name) {
this.gname = name;
}
public void etc(){
System.out.format("The name of your true crush was %s", getName());
}
}
What is wrong with my code? Compiler says non static variable cannot be referenced from a static context
package nestedclass;
public class Nestedclass {
class Student {
String name;
int age;
long roll;
public Student(String name, int age, long roll) {
this.name = name;
this.age = age;
this.roll = roll;
}
public void show() {
System.out.println("name : "+name+" age : "+age+" roll : "+roll);
}
}
public static void main(String[] args) {
//Nestedclass=new Nestedclass();
Student ob=new Student("ishtiaque",10,107060);
ob.show();
// TODO code application logic here
}
}
The nested Student class isn't static, that's why the compiler complains. There are a couple of ways to get out of this situation: make Student static, or instantiate Nestedclass and provide a nonstatic method in Nestedclass that does the actual work on an instance of Student, and that you call from main:
private void run() {
Student ob = new Student("ishtiaque", 10, 107060);
ob.show();
}
public static void main(String[] args) {
new Nestedclass().run();
}
or, if you like oneliners you could also do
public static void main(String[] args) {
new Nestedclass().new Student("ishtiaque", 10, 107060).show();
}
Personally I prefer the second method (with the helper method) as it's easier to refactor afterwards.
Your inner class is not declared as static; therefore, you cannot access it from the static method main. Change your inner class declaration to static class Student
You need to initiate the static class then Use the new of that class in order to initiate the their inner none classes. Or you can declare your Student inner class as static class.
first solution
Student ob = new Nestedclass().new Student("ishtiaque", 10, 107060);
second solution
static class Strung {...}
Hope that helps.
this will work
public class Student
{
String name;
int age;
long roll;
public Student(String name, int age, long roll) {
this.name = name;
this.age = age;
this.roll = roll;
}
public void show()
{
System.out.println("name : "+name+" age : "+age+" roll : "+roll);
}
public static void main(String[] args) {
Student ob=new Student("ishtiaque",10,107060);
ob.show();
}
}
I am a complete beginner in java, so please forgive me if this question is not up to the standard of this website:
class person{
String name;
int age;
}
class teacher extends person{
person s1=new person();
teacher t1=new teacher();
t1.age=56;
}
Here I am trying to access the variable age and name of class to assign them values person, which happens to be the super class of person. But the compiler is giving error. I even tried to make the name variable and age variable as public. But the compiler is still reporting an error. I want to know the reason why I can't access superclass variable in subclass directly and assign values to them.
You are not allowed to write arbitrary code directly within a class body. The closest thing to what you have written is
class teacher extends person{
person s1=new person();
teacher t1=new teacher();
{
t1.age=56;
}
}
This is called the instance initializer block.
In general, it is not a good idea to access variables directly. Consider using something like the code below.
Test driver
package com.example.input;
public class TestPeople {
public static void main(String[] args) {
Person s1 = new Person();
s1.setName("student 1").setAge(19);
Teacher t1 = new Teacher("Dr. Fun",0);
t1.setAge(56);
System.out.println("Student " + s1.getName() + ", " + s1.getAge());
System.out.println("Teacher " + t1.getName() + ", " + t1.getAge());
}
}
Class Person
package com.example.input;
public class Person {
private String name;
private int age;
public Person() {};
public Person(String aName, int anAge) {
setName(aName).setAge(anAge);
}
public String getName() {return name;}
public int getAge() {return age;}
public Person setName(String aName) { name = aName; return this;}
public Person setAge(int anAge) { age = anAge; return this;}
}
Class Teacher
package com.example.input;
public class Teacher extends Person {
public Teacher() {
super();
}
public Teacher(String aName, int anAge) {
super(aName, anAge);
}
}
Actually you are making an assignment of variable of external class in existing class body which is not legal.Make the assignment in a method or constructor or in a anonymous block.
class person{
String name;
int age;
}
class teacher extends person{
person s1=new person();
teacher t1=new teacher();
// t1.age=56; Error here
public teacher()
{
t1.age=56;
}
//or method
public void setAge()
{
t1.age=56;
}
}
do whatever suits your program and you.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
public class Main {
public static void main(String[] args) {
Person Gunnar = new Person();
Person Sven = new Person();
Dog Doggy = new Dog();
Dog Doggi = new Dog();
Gunnar.setName("Gunnar");
Gunnar.setAge(20);
Gunnar.setHeight(215);
Sven.setName("Sven");
Sven.setAge(55);
Sven.setHeight(178);
Sven.sayHello(Gunnar);
}
}
Java wants me to change Sven and Gunnar (name of the two Person objects) to Person in the .setName lines so it would be Person.setName("Gunnar"); and Person.setName("Sven");. But, that won't work since there would be no way of telling which one is which. Please correct me if I am wrong. Also don't mind the dog code.
Here's the code for the Person class:
public class Person {
static int age;
static int height;
static String name;
public void sayHello(Person name) {
System.out.println("Hello!" +name);
}
public int getAge() {
return age;
}
public void setAge(int age) {
Person.age = age;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
Person.height = height;
}
public static String getName() {
return name;
}
public static void setName(String name) {
Person.name = name;
}
}
You are calling your methods correctly (from logical point of view)
but apparently you have declared them incorrectly (as static).
Make them instance methods by removing the static modifier.
That should fix your problem. And learn the the difference between
static and instance methods (seems you haven't quite yet).
In particular getName and setName should not be static.