Home > front end >  Why can't I declare object in my local class in Java?
Why can't I declare object in my local class in Java?

Time:05-15

I started learning programming recently. I know it can look meaningless, but I want to know reason why I can't declare my object in my local class. I can declare it on the second class that I create, but can't declare in the main one.

Here is my code :

public class User {
    
 public String name;
 public String surname;
 public int age;
 public static String companyname;
 
 User student = new User();
 student.name = "Jack";
}

CodePudding user response:

The error that your IDE is giving you, It's about your statement

student.name = "Jack";

You can declare a User, as you're doing here

User student = new User();

It will be interpreted as an attribute of your User class. However, you cannot write any logic outside a method. You can only declare fields in order to define the class' attributes. If you want to assign the name Jack to your student variable you have three options:

  1. Define a constructor which accepts a name and pass the value Jack to your student instance during its instantiation.
public class User {

    public String name;
    public String surname;
    public int age;
    public static String companyname;
    User student = new User("Jack");
    
    public User(String name){
        this.name = name;
    }
}
  1. Include your statement within an instance block.
public class User {

    public String name;
    public String surname;
    public int age;
    public static String companyname;
    User student = new User();
    {
        student.name = "Jack";
    }
}
  1. Initialize your student field inside the class constructor.
public class User {

    public String name;
    public String surname;
    public int age;
    public static String companyname;
    User student = new User();

    public User(){
        this.student.name = "Jack";
    }
}

However, all these options have little meaning and they were all meant to let you understand the contexts where you can write that type of statement. Besides, as reported in the comments, creating a new User within your User class will overflow the stack every time a new User is created, as the creation of each User triggers the creation of another one.

I imagine that you simply wanted to define a User with its actual fields (name, surname, age, companyname) and then declare a Student in a "testing" context, like a main method. This is most likely what you were attempting to do:

public class User {

    public String name;
    public String surname;
    public int age;
    
    public static String companyname;

    public User(String name, String surname, int age) {
        this.name = name;
        this.surname = surname;
        this.age = age;
    }

    public static void main(String[] args) {
        User student = new User("Jack", "Black", 15);
    }
}

On a side note, you should define a class' attributes (its fields) as private. The public access modifier is generally meant for the services offered by an object (its methods). This is also known as information hiding or encapsulation. Basically, you want to hide the internal implementation of your class and protect its state (its fields) from any abuse, like assigning inconsistent values. Your methods are the "outer layer" to regulate the access to your internal state, they contain the logic to prevent misuses and inconsistent changes to your object's state.

No Encapsulation

public class User {

    public String name;
    public String surname;
    public int age;
   
    public User(String name, String surname, int age) {
        this.name = name;
        this.surname = surname;
        this.age = age;
    }

    public static void main(String[] args) {
        User student = new User("Jack", "Black", 15);
        
        //misuse of the object's attributes
        student.age = -20;
    }
}

Encapsulation

public class User {

    private String name;
    private String surname;
    private int age;

    public User(String name, String surname, int age) {
        this.name = name;
        this.surname = surname;
        this.age = age;
    }

    public void setAge(int age) {
        if (age < 0 || age > 120) {
            return;
        }
        this.age = age;
    }

    public int getAge() {
        return age;
    }

    public static void main(String[] args) {
        User student = new User("Jack", "Black", 15);

        student.age = -20; //gives you an error, you cannot access anymore this field directly

        student.setAge(-20); //It won't update its age to -20
        System.out.println(student.getAge()); //It still prints 15

        student.setAge(25); //Regulates the update and allows the assignment
        System.out.println(student.getAge()); //Prints 25
    }
}

This article explains quite well the concept of encapsulation:

https://www.geeksforgeeks.org/encapsulation-in-java/

  • Related