In general you should not access object's attributes directly it should be done through setter
and getter
methods. So you should define the class
you want with the attributes needed and their setter
and getter
methods. for example some thing like:
public class MyClass
{
private string myField; //"private" means access to this is restricted
public string getMyField()
{
//include validation, logic, logging or whatever you like here
return this.myField;
}
public void setMyField(string value)
{
//include more logic
this.myField = value;
}
}
Quoted from: tutorial on getters and setters?
After that you need to create new object of that class
and set its attribute's value through the setter
methods where the value is an user input.
MyClass myClassObj=new MyClass(); // creating new instance of MyClass
System.out.println("Please enter an ID: ");
Scanner scan= new Scanner(System.in);
String userInput=scan.nextLine(); // get user input
myClassObj.setMyField(userInput); // setting myFiled attribute
and just for testing purpose you can get myFiled
value through getter
method as follow:
System.out.println("The data enterd by user is: "+myClassObj.getMyFiled());