problem on create a new class
已回答
when i create a class to separate the objects and then when i call on the Main class, it doesnt recognize and keep getting ''cannot resolve symbol'', the code only works if i put everything on the main class
main class
package com.company;
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
User person1 = new User();
person1.name = "Fulano";
person1.birthdateUser = LocalDate.of(1970, 5, 17);
person1.age = LocalDate.now().getYear() - person1.birthdateUser.getYear();
System.out.println(pessoa1.getNome() + " tem " + pessoa1.getIdade() + " anos");
}
}
users class
import java.time.LocalDate;
public class Users {
public static class User {
private String name;
private LocalDate birthdateUser;
private int age;
public User(){
}
public Usuario(String name, LocalDate birthdateUser, int age) {
this.name = name;
this.birthdateUser = birthdateUser;
this.age = LocalDate.now().getYear() - birthdateUser.getYear();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public LocalDate birthdateUser() {
return birthdateUser;
}
public void setbirthdateUser(LocalDate birthdateUser) {
this.birthdateUser = birthdateUser;
}
public int getage() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
}
请先登录再写评论。
Your code is not valid, we've replied with more details in the support ticket. If you need help with your Java code, please ask at https://stackoverflow.com/. It's not IntelliJ IDEA related problem.
but what am i doing wrong, it works if i put everyhthing in main class, am i forgetting an import?
The main issue is that you can't import classes from the default package in Java: https://stackoverflow.com/questions/2030148/whats-the-syntax-to-import-a-class-in-a-default-package-in-java .
The other issue is that your code is in the inner class instead of the top level class in the same package.
Yet another issue is that you should use getters/setters as the fields you are trying to access are private.