Java OOP; creating several objects - java

I am trying to store students' data in main class, in the student class I did the following:
public class Student {
public static String name = "UNKNOWN";
Student(){
}
Student(String name) {
this.name = name;}
While in main I did the following:
public class Main {
public static void main (String[] args) {
//s1 is short for student1
Student s1 = new Student ("Chris");
Student s2 = new Student ("Layla");
Student s3 = new Student ("Mark");
This issue is, whenever I print a sx.name I'd always print the last one. So for the following code:
System.out.println(s1.name);
I'd get Mark, while it should be Chris.

Yes, Because static fields are shared by all the objects.
Please change your code as follows:
public class Student {
public String name;
Student(){
}
Student(String name) {
this.name = name;
}
public static void main (String[] args) {
//s1 is short for student1
Student s1 = new Student ("Chris");
Student s2 = new Student ("Layla");
Student s3 = new Student ("Mark");
}
When to use static in java?
Sometimes, you want to have variables that are common to all objects.
This is accomplished with the static modifier. Fields that have the
static modifier in their declaration are called static fields or class
variables. They are associated with the class, rather than with any
object. Every instance of the class shares a class variable, which is
in one fixed location in memory. Any object can change the value of a
class variable, but class variables can also be manipulated without
creating an instance of the class.

The static variable name is accessed by instances, which is not recommended in Java. You can make it non-static. The IDE, such as IDEA, will give you some hint/warning about this issue, like:
So with the help of IDEs, you can easily find some potential issues.

Related

Using an object within its own constructor

Is it possible (or sensible) to use an object in it's own constructor?(Sorry for the poorly formulated noob question)
Say I have a class "Students" which contains an arrayList of subclass Student and a method for adding new students to the array.
Can I in my Student constructor use the addStudent method to add the new instance to the array on creation?... like so:
//Students
class Students{
private static ArrayList<Student> STUDENTS = new ArrayList<>();
public static void addStudents(Student student){
STUDENTS.add(student);
}
}
//Student
class Student /*extends Students <- old misstake left for reference*/{
private String name = "";
private int birthYear = 0;
Student(String _name, int _birthYear){
this.name = _name;
this.birthYear = _birthYear;
//insert wild guess
Students.addStudents(this(name,birthYear));
}
}
Or will this simply loop and create a lot of objects until everything crashes?
You can; you just shouldn't.
One reason is that you might not always want to add all Student instances to the same shared list. For example, if you're creating Student instances in a unit test, and adding them into the Students list in the constructor, you then have to worry about clearing the list after the test, to avoid accidentally sharing state between tests.
Another reason is that adding the instance in the constructor is called unsafe publication. You are giving out a reference to an instance which has not been fully initialized. This can lead to some very tricky bugs, especially related to concurrency.
You should always wait until an instance has been fully initialized (i.e. one new Whatever has returned) before you do anything with it.
You would be better decoupling creating the Student from adding to the Students list. Use a factory method to create students:
class Students{
private static ArrayList<Student> STUDENTS = new ArrayList<>();
public static void addStudents(Student student){
STUDENTS.add(student);
}
// Factory method.
public static Student createAndAddStudent(String name, int birthYear) {
Student student = new Student(name, birthYear);
addStudents(student);
return student;
}
}
In terms of your current code, you don't need extends Students. Logically, Student isn't a Students, any more than a Car wouldn't be a Cars (say it out loud; it just doesn't make sense).
All you need to do is to invoke the static method:
class Student {
Student() {
// ...
Students.addStudents(this);
// ...
}
}
You are using the keyword this incorrectly. You don't send any values along with this. this is a reference to the current object.
The benefit of using this way is that you'll never had to individually add all of the students to a list. Along with this it will save you the hassle of accidentally forgetting to add one to your array.
class Student extends Students{
private String name = "";
private int birthYear = 0;
Student(String _name, int _birthYear){
this.name = _name;
this.birthYear = _birthYear;
addStudents(this);
}
public static void main(String[] args){
Student s = new Student("Ryan", 1999);
}
}

How to access superclass variables from user input

I am trying to access the super class variable name from user input.
I am not sure how to have the super class variable name point to the user input. Here is the code for it. Any ideas thank you.
package chapter4;
import java.util.Scanner;
public class VetOffice extends Animal {
public VetOffice(int lifeExpectancy, int weight, String name, Character gender, String type) {
super(lifeExpectancy, weight, name, gender, type);
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.print("Please enter name of pet");
//super(name);
//= console.next();
//}
}
}
//}
The thing is you cannot access super class variables in main method. Because it is static method. If you want to access in main() you have to make Animal class name variable to static. Then you can assgin a value directly in main().
Like this:
in Animal class,
static String name;
in VetOffice,
name = console.next();
You can try different ways depending on the thing that you are going to achieve you have to decide,
Is this variable can be declare as static or not? Because static variables common for every object.
Another way you can do this is,
Create getters and setters for Animal class member variables. Then also you cannot access in the main method because also you have to make those methods and variable to static.
As a solution without makinng them static or a new methods even getters and setters in super class you can create default constructor for super class and assign values like below:
If your variable in the super class is private you have to create getters and setters.
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.print("Please enter name of pet");
VetOffice pet = new VetOffice();
pet.name = console.next();
System.out.println(pet.name);
}
Note: create default constructor If it is unnecessary to create object from VetOffice or super class seeing that you have to pass values to constructor.
UPDATE:
According to your comment
If your variable in the super class is private do this:
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.print("Please enter name of pet");
VetOffice pet = new VetOffice();
pet.setName(console.next());
System.out.println(pet.getName());
}
Another way that you asked for in the comment:
Animal class(partialy implemented to show you)
public class Animal {
int lifeExpectancy;
static int weight;
static String name;
Animal(int lifeExpectancy, int weight, String name, Character gender, String type){
this.weight = weight;
this.name = name;
}
public static String getName() {
return name;
}
public static void setName(String n) {
name = n;
}
}
Then in the main method:
Animal.setName(console.next());
System.out.println(Animal.getName());

ArrayList of class as parameter

StackPeople, I have a question. What statement could help me implement the right class before inserting it to the ArrayList. I have declared Nurse and Pilot which are Employees objects.
I want each implementation of my class ArrEmp to store different Employees objects
example: arrEmpNurses, arrEmpPilots,... after my class gets an example in the constructor
What statement helps?? Or should I re think the problem.
Thanks for your help.
THE PROBLEM IS TO FILL THE ARRAY WITH THE RIGHT CLASS (IT WILL READ FROM PLAIN TEXT AND IT NEWS TO BE NOTIFIED WhAT CLASS TO IMPLEMENT TO ADD IT)
"This code compiles, just copy paste."
import java.util.*;
public class ArrEmp {
String[][] data={ {"E1"}, {"Maria"}, {"E2"}, {"John"} }; //Data
Employee x;
static Nurse nancy= new Nurse("01","Nancy");//this are just examples
static Pilot peter= new Pilot("02","Peter");//so the arrayEmp knows what type of employee create
ArrayList arr;
public ArrEmp(Employee x){
this.x=x;
arr= new ArrayList();
fillList();//with data array
}
public void fillList(){// I would like to fill the List with Nurses. How could i do it?
//for( String[] param: data )
//arr.add( ) //insert helpfull statement here
//the goal is to have an array of Pilot and another of Nurses
}
public static void main(String[] args) {
ArrEmp arr1= new ArrEmp( nancy );
ArrEmp arr2= new ArrEmp( peter );
}
public static class Employee {
String cod;
public Employee(String cod){
this.cod=cod;
}
}
public static class Nurse extends Employee{
String name;
public Nurse(String ... para){
super(para[0]);
this.name=para[1];
}
}
public static class Pilot extends Employee{
String name;
public Pilot(String ... para){
super(para[0]);
this.name=para[1];
}
}
}
I asked the question this way because data is actually read from Disk and ArrEmp has no idea what Employee he is reading. i need to provide an example so it builds the right employee and then insert it into the array. so new ArrEmp( nancy ) reads the file and builds Nurses and store them but new ArrEmp( nancy ) reads a file and loads Pilots on it.
EDIT SOLUTION: ESCENTIALLY I WILL CREATE A GENERIC ARRAYLIST EXTENDS EMPLOYEE, and extending classes for each Emlployee object...
Why not use generics? See: Java generics - ArrayList initialization
Essentially use
ArrayList<Nurse>
Instead of ArrayEmp(Nancy) to say it will only contain Nurses, then the language will take care of enforcing it.
public static class Employee {
String name;
int ID = 0;
public Employee(String name){
this.name = name;
}
}
Just use ID's to denote the differentiation between all of them? You can create an ENUM and fill in legible names for differentiating between different objects. It's faster then string comparing and using instanceOf.
public static class Pilot extends Employee{
int ID = 1;
public Pilot(String name){
this.name = name;
}
}
EDIT:
public ArrEmp(Employee x){
if (x.ID == 1) // add to the list you want
else if (x.ID == 2) // add to list you want
....
}

Class inside a class java?

Im doing a question that requires you to make a class customers which will later on be added into an array list in the method of another class. However I am getting an error on the line i marked ERROR, that says:
"No enclosing instance of type Question3 is accessible. Must qualify the allocation with an enclosing instance of type Question3 (e.g. x.new A() where x is an instance of Question3)." And I have no clue why.
public class Question3 {
static ArrayList<customers> a= new ArrayList<customers>();
private static Scanner kbd;
public static void main(String[] args)
{
String input="";
double price=1;
String name="";
while(price != 0)
{
System.out.println("Customer Name: ");
name= kbd.nextLine().trim();
System.out.println("Purchase Price: ");
price= Double.parseDouble(kbd.nextLine().trim());
addSale(name,price); //ERROR
}
}
public static void addSale(String name, double price)
{
customers c= new customers(name,price);
a.add(c);
}
public class customers
{
String name;
double price;
public customers(String name, double price)
{
this.name=name;
this.price=price;
}
}
}
You also have to initialize the kbd variable as:
kbd = new Scanner( System.in );
Please review your code using this suggestion and the others above.
A main method is static and thus has static context. No instance of Question3.class is required for a thread to enter that code block. Your class customers is defined inside of Question3. Because it is an inner class, it has implicit access to the fields and methods inside of the Question3 class, but it requires an instance of Question3 to be able to achieve that behavior. You need to move the code you have now in main(String args[]) into a constructor for the class Question3, and create an instance of Question3 in your main method like so :
public static void main(String args[]) {
Question3 myQuestion3 = new Question3();
}
Alternatively as mentioned by others, you could make your customers class static. This will solve the issue by effectively making customers a top level class, but you will lose the ability to implicitly access the fields and methods of its enclosing type, which is the Question3 class.
First off Great job so far. However, there are a couple of errors that I see in the code.
First you class should be a static class. You are trying to use static methods without a static class.
public static class Question3 {
static ArrayList<customers> a= new ArrayList<customers>();
private static Scanner kbd;
public static void main(String[] args)
{
Also, you need to create your scanner for the user to input an object.
private static Scanner kbd = new Scanner(System.In);
Do these and your code will work perfectly!
You should change the declaration your class customers to solve this issue.
Currently its a non-static inner class. You should change it to static inner class.
public static class customers
Non-static inner classes refers implicitly to the instance of the container class. Here you trying to create new instance of customer class in a static function, you don't have Question3 instance there.
Just change your inner class to a public static class:
public static class customers {
And the error disappears :)
There are two problems in your code.
First , you have to initialize scanner object by providing System.in parameter to it.
Second , while creating customer object you have to follow proper syntax.
Here is the working code:
public class Question3 {
static ArrayList<customers> a= new ArrayList<customers>();
private static Scanner kbd=new Scanner(System.in); // <---- Notice this
public static void main(String[] args)
{
String input="";
double price=1;
String name="";
while(price != 0)
{
System.out.println("Customer Name: ");
name= kbd.nextLine().trim();
System.out.println("Purchase Price: ");
price= Double.parseDouble(kbd.nextLine().trim());
addSale(name,price); //ERROR
}
System.out.println(a);
}
public static void addSale(String name, double price)
{
// customers c= new customers(name,price);
Question3.customers c = new Question3().new customers(name, price); // <---Notice this
a.add(c);
}
public class customers
{
String name;
double price;
public customers(String name, double price)
{
this.name=name;
this.price=price;
}
} }

Make 3 classes call one another

So I'm trying to figure out how can I have 3 classes call one another.
this is the main class.
public class TestStudent {
public static void main(String[] args) {
myStudent mystudent_obj = new myStudent();
mystudent_obj.show_grades();
mystudent_obj.change_grades();
mystudent_obj.show_grades();
}
}
This is the 2nd class that's being called in the class above;
The 2nd class call another 3rd class and try to manipulate it
using two functions. Function show_grades just print out the variables in the 3rd class
and function change_grade try to change the variables in the 3rd class.
public class myStudent {
public void show_grades(){
Student student_obj = new Student();
System.out.println(student_obj.studGrade);
System.out.println(student_obj.studID);
}
public void change_grades(){
Student student_obj = new Student();
student_obj.studGrade='V';
student_obj.studID=10;
}
}
This the 3rd call, which only has two variables.
public class Student {
public int studID = 0;
public char studGrade = 'F';
}
when I run the program it runs without errors and I get an output of:
F
0
F
0
however, I can see that the function show_grades work and it does display the grades, but
the function change_grades does not change the grades:
The end results, should be something like this
F
0
V
10
because the change grade function, should have changed those variables... so what's going on?
In your myStudent class you are creating a new instance of Student in each method, meaning that each method has a local variable of class Student. When you call show_grades the second time, a new instance is created, with the default values of 0 and F.
If you create a variable and use that instead, your change grades will change the variables of the instance variable instead of a local variable in each method. This is due to scoping in programming, which you can read more about at Wikipedia.
public class myStudent {
private Student student_obj = new Student();
public void show_grades() {
System.out.println(student_obj.studGrade);
System.out.println(student_obj.studID);
}
public void change_grades(){
student_obj.studGrade='V';
student_obj.studID=10;
}
}

Categories