Updating & Retrieving Private Variables from Postman Class in Worker

In summary, to update and retrieve the private variables Name and Age from another class Worker, you can create accessor methods in the Postman class that allow you to get and set the private attributes. This will allow you to access and modify the variables from the Worker class.
  • #1
kloong
36
0
Question:
public class Postman {
private String Name;
private int Age;
}

How you can update and retrieve the private variables Name and Age from another class Worker.

public class Worker {
//An object a is created from class Postman

What i tried to do:
//Postman.java
public class Postman{
private String name;
private int age;
}

//constructor
public Postman(String name, int age){
this.name=name;
this.age=age;
}

//accessors
public String getName(){
return name;
}

public int getAge(){
return age;
}

//worker.java
public class worker{
public static void main(String args[]){
Postman employee = new employee("Tom the postman", 55);
System.out.println(employee.getName(…
System.out.println(employee.getAge()…
}

}



But when i buid it, it keeps giving me the class, interface, or enum expected error. Why is that?
 
Physics news on Phys.org
  • #2
Answer:To update and retrieve the private variables Name and Age from another class Worker, you can create accessor methods in the Postman class. Accessor methods are methods that allow you to get and set the private attributes of a class. The following code shows an example of how to do this: //Postman.javapublic class Postman{private String name;private int age;//constructorpublic Postman(String name, int age){this.name=name;this.age=age;}//accessorspublic String getName(){return name;}public void setName(String name){this.name = name;}public int getAge(){return age;}public void setAge(int age){this.age = age;}}//worker.javapublic class worker{public static void main(String args[]){Postman employee = new employee("Tom the postman", 55);System.out.println(employee.getName());System.out.println(employee.getAge());//To update the name and ageemployee.setName("John the postman");employee.setAge(60);System.out.println(employee.getName());System.out.println(employee.getAge());}}
 
  • #3


It appears that you have not properly defined the class and constructor in your worker.java file. Make sure that the class name and constructor name match those in your Postman.java file. Additionally, make sure that you have imported the Postman class into your worker.java file. Once these steps are completed, you should be able to successfully update and retrieve the private variables from the Postman class in your worker class.
 

1. How do I update private variables in a Postman Class in Worker?

To update private variables in a Postman Class in Worker, you can use the "update" keyword followed by the variable name and the new value. For example, if the private variable is named "count" and you want to update it to 10, you would use "update count 10". This will update the value of the private variable to 10.

2. Can private variables be retrieved from a Postman Class in Worker?

Yes, private variables can be retrieved from a Postman Class in Worker. To retrieve the value of a private variable, you can use the "get" keyword followed by the variable name. For example, if the private variable is named "username" and you want to retrieve its value, you would use "get username". This will return the value of the private variable.

3. How can I access private variables from outside the Postman Class in Worker?

Private variables cannot be accessed from outside the Postman Class in Worker. They are only accessible within the class where they are declared. This is to ensure data privacy and prevent unintended changes to the variables.

4. Is it possible to update multiple private variables at once in a Postman Class in Worker?

Yes, it is possible to update multiple private variables at once in a Postman Class in Worker. You can use the "update" keyword followed by the variable names and their new values separated by spaces. For example, if you want to update the values of "count" and "total" to 10 and 100 respectively, you would use "update count 10 total 100". This will update both variables to their respective values.

5. Are private variables in a Postman Class in Worker accessible to all requests?

No, private variables in a Postman Class in Worker are only accessible to requests within the same instance of the class. Each request creates a new instance of the class, so private variables are not shared between requests. This allows for data isolation and prevents conflicts between different requests.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Programming and Computer Science
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Programming and Computer Science
Replies
3
Views
3K
Back
Top