Referencing a non static array list in a static environment? - java

I've just started to learn Java a month or so ago, and now have a problem with "non-static variable studentList cannot be referenced from a static context". I'm trying to have a separate method from main to populate the list of students, instead of copy pasting stuff from addStudent for each student; but I cannot get the methods to write to the ArrayList. (Error:(14, 27) java: non-static variable studentList cannot be referenced from a static context). I understand how the array is not static because it has an undefined size, but how could I make it work as is? Is there any better approach? Could I have the array be part of the main method and then have it passed on to addStudent, if so how?
import java.util.ArrayList;
public class Main {
ArrayList<Student> studentList = new ArrayList<>();
public static void main(String []args) {
addStudent("Adam", "Goldsmith", 70, 50);
addStudent("John", "Smith", 20, 40);
addStudent("Lewis", "Peterson", 90, 85);
for (Student obj: studentList){
System.out.println("Name: " + obj.studentForename + " "+ obj.studentSurname);
}
}
public static void addStudent(String forename, String surname, int coursework, int test) {
Student newStudent = new Student(forename, surname);
newStudent.setForename(forename);
newStudent.setSurname(surname);
newStudent.averageMark(70, 65);
studentList.add(newStudent);
}
}
and my "Student" Class:
public class Student {
String studentForename;
String studentSurname;
public Student(String studentForename, String studentSurname) {
setForename(studentForename);
setSurname(studentSurname);
}
// Set forename.
public void setForename(String newForename) {studentForename = newForename;}
// Set surname.
public void setSurname(String newSurname) {studentSurname = newSurname;}
//
public double averageMark(int courseworkMark, int testMark){
return (courseworkMark+testMark)/2;
}
// Grab the forename
public String grabForename(){
return studentForename;
}
// Grab the surname
public String grabSurname(){
return studentSurname;
}
// Grab the full name
public String grabFullName(){
return studentForename + "" + studentSurname;
}
}

Your code should look like this, especially your Student class using java encapsulation
public class Student {
private String studentForename;
private String studentSurname;
private int courseworkMark;
private int testMark;
public Student(String studentForename, String studentSurname, int courseworkMark, int testMark) {
this.studentForename = studentForename;
this.studentSurname = studentSurname;
this.courseworkMark = courseworkMark;
this.testMark = testMark;
}
public void setStudentForename(String studentForename) {
this.studentForename = studentForename;
}
public String getStudentSurname() {
return studentSurname;
}
public void setStudentSurname(String studentSurname) {
this.studentSurname = studentSurname;
}
public String getStudentForename() {
return studentForename;
}
public double averageMark(){
return (this.courseworkMark + this.testMark)/2;
}
public String grabFullName(){
return studentForename + " " + studentSurname;
}
}
And then testing via your Main class :
public class Main {
public static void main(String []args) {
ArrayList<Student> studentList = new ArrayList<>();
studentList.add(new Student("Adam", "Goldsmith", 70, 50));
studentList.add(new Student("John", "Smith", 20, 40));
studentList.add(new Student("Lewis", "Peterson", 90, 85));
for (Student obj: studentList){
System.out.println("Name: " + obj.getStudentForename() + " "+ obj.getStudentSurname());
}
}
}

import java.util.ArrayList;
public class Main {
static ArrayList<Student> studentList = new ArrayList<>();
public static void main(String []args) {
addStudent("Adam", "Goldsmith", 70, 50);
addStudent("John", "Smith", 20, 40);
addStudent("Lewis", "Peterson", 90, 85);
for (Student obj: studentList){
System.out.println("Name: " + obj.studentForename + " "+ obj.studentSurname);
}
}
public static void addStudent(String forename, String surname, int coursework, int test) {
Student newStudent = new Student(forename, surname);
newStudent.setForename(forename);
newStudent.setSurname(surname);
newStudent.averageMark(70, 65);
studentList.add(newStudent);
}
}
It was not due to undefined size but was because you were trying to access it without creating an object from a static method.
So just write static before it and it will work.

thought the above answer answers you question, but few words about static vs non static modifiyers in java
Characteristics of Static methods
A static method is called using the class (className.methodName) as opposed to to an instance reference (new instanceOfClass = class; instanceOfClass.methodName.)
Static methods can’t use non-static instance variables: a static method can’t refer to any instance variables of the class. The static method doesn’t know which instance’s variable value to use.
Static methods can’t call non-static methods: non-static methods usually use instance variable state to affect their behaviour. Static methods can’t see instance variable state, so if you try to call a non-static method from a static method the compiler will complaint regardless if the non-static method uses an instance variable or not.
Non-Static methods
A non-static method does not have the keyword static before the name of the method.
A non-static method belongs to an object of the class and you have to create an instance of the class to access it.
Non-static methods can access any static method and any static variable without creating an instance of the class.
So you'd better think if you need to define studentList as static or no, and if modify your code accordingly.
P.S. above written is taken from here

The difference between static (global, class level) and non-static (a instance of that class, an object) is important.
Creating an object by new Main() allows to work on that object and its fields.
The static void main(String[]) is the single entry point to the application.
Inside the main you have access only to static fields and static methods. So that is cumbersome.
package srivastava.arpit; // In a directory path srivastava/arpit/
import java.util.ArrayList;
public class Main {
private ArrayList studentList = new ArrayList<>();
public static void main(String []args) {
Main main = new Main();
main.execute();
}
private void execute() {
addStudent("Adam", "Goldsmith", 70, 50);
addStudent("John", "Smith", 20, 40);
addStudent("Lewis", "Peterson", 90, 85);
for (Student obj: studentList){
System.out.println("Name: " + obj.studentForename + " "+ obj.studentSurname);
}
}
public void addStudent(String forename, String surname, int coursework, int test) {
Student newStudent = new Student(forename, surname);
newStudent.setForename(forename);
newStudent.setSurname(surname);
newStudent.averageMark(70, 65);
studentList.add(newStudent);
}
}

Related

Java cannot find symbol when calling a method from another class

Im trying to call the method show in the klub class, from my demo class, and to call the method visAlleAtleter in the demo class (from my klub class), but I get an error saying it can't find the symbol and then the name of the method. I have tried to specify where I want it from, but I'm not certain how to do it. This is my Klub class
import java.util.ArrayList;
public class Klub {
private String navn;
private ArrayList<Atlet> atleter;
public Klub(String navn) {
this.navn = navn;
atleter = new ArrayList<>();
}
public void addAtlet(Atlet atlet) {
atleter.add(atlet);
}
public void visAlleAtleter() {
System.out.println(navn);
for(Atlet atlet: atleter) {
atlet.display();
}
}
public void visAlleAtleter2(boolean samletValue) {
if(samletValue = true) {
Atlet.show();
} else {
}
}
}
this is my Demo class
public class Demo {
public static void main(String[] args) {
Klub minKlub = new Klub("SWU United");
Atlet atlet1 = new Atlet("Pan", "Tennis", 43.5, 21);
Atlet atlet2 = new Atlet("Peter", "Golf", 41.5, 29);
Atlet atlet3 = new Atlet("Robby", "Tennis", 43.5, 20);
Atlet atlet4 = new Atlet("Niklas Klein", "Maisball", 21.2, 23);
Atlet atlet5 = new Atlet("Linni Maister", "Maisball", 44.1, 32);
Atlet atlet6 = new Atlet("Dennis Michael", "Maisball", 32.5, 11);
minKlub.addAtlet(atlet1);
minKlub.addAtlet(atlet2);
minKlub.addAtlet(atlet3);
minKlub.addAtlet(atlet4);
minKlub.addAtlet(atlet5);
minKlub.addAtlet(atlet6);
Klub.visAlleAtleter();
}
}
and my atlet class
public class Atlet {
private String navn;
private String sportsgren;
private double pris;
private int alder;
public Atlet(String navn, String sportsgren, double pris, int alder) {
this.navn = navn;
this.sportsgren = sportsgren;
this.pris = pris;
this.alder = alder;
}
public void oppdaterPris(double nyPris) {
pris = nyPris;
}
public double getPris() {
return pris;
}
public double predSalgspris() {
return pris-0.95*Math.abs(25-alder);
}
public void display() {
System.out.println(navn + " (" + alder + ") - " +
sportsgren + ": " + pris + "kr (" + predSalgspris() + "kr)");
}
}
Method visAlleAtleter is not static so your can't call it as you do Klub.visAlleAtleter().
Just call method on object minKlub.visAlleAtleter().
The problem lies in the line Klub.visAlleAtleter(); in the main method in Demo. To correct this you have to replace it with minKlub.visAlleAtleter();.
The reason for this is that according to your code, you have an instance of the class Klub called minKlub. The method visAlleAtleter() is bound to any instance of the class Klub, not the class Klub itself.
EDIT:
To illustrate why your attempt could be an issue, consider this example:
public class Demo {
public static void main(String[] args) {
Klub minKlub = new Klub("SWU United");
Klub dinKlub = new Klub("SEU United")
Atlet atlet1 = new Atlet("Pan", "Tennis", 43.5, 21);
Atlet atlet2 = new Atlet("Peter", "Golf", 41.5, 29);
minKlub.addAtlet(atlet1);
minKlub.addAtlet(atlet2);
Atlet atlet3 = new Atlet("Mads", "Fotball", 123, 123);
Atlet atlet4 = new Atlet("Mikkelsen", "Curling", 123, 123);
dinKlub.addAtlet(atlet3);
dinKlub.addAtlet(atlet4);
Klub.visAlleAtleter();
}
}
In this example, when calling Klub.visAlleAtleter(), it is impossible to know whether Java should call the method on minKlubb or dinKlubb. Its important that you see minKlub and dinKlub as physical instances of the Klub class.
Coincidentally, writing Klub.someArbitraryMethod() is actually a commonly used feature in java, where someArbitraryMethod() is statically declared. This way of writing is only possible if you declare the method as static:
public static void someArbitraryMethod() {...}
This means that the method doesn't belong to any physical instances of the class Klub, but rather it belongs to the class Klub itself. Doing
minKlubb.someArbitraryMethod();
will therefor cause a compilation error.

[Java]Why am I getting these errors in my tester class?

I have a super class, sub class, and a tester class. Can anyone help with what I'm doing wrong?
Here is what I need in my tester class-
1. Create objects of Manager and Employee.
Create a function revise salary to revise the salary set in the constructor of Manager or Employee. (Use concept of polymorphism)
Use the object’s Display functions to print the relevant information pertaining to the object’s class.
Here are my classes
Superclass:
public class Employee {
private int employeeNumber;
private String employeeName;
private double employeeSalary;
public Employee(int employeeNumber, String employeeName, double employeeSalary) {
this.employeeNumber = employeeNumber;
this.employeeName = employeeName;
this.employeeSalary = employeeSalary;
}
public double getEmployeeSalary() {
return employeeSalary;
}
public void setEmployeeSalary(double employeeSalary) {
this.employeeSalary = employeeSalary;
}
public void display(){
System.out.println("Employee Number: "+ employeeNumber +"\n"
+ "Employee Name: " + employeeName + "\n"
+ "Employee Salary: " + employeeSalary);
}
}
Subclass:
public class Manager extends Employee {
private int rewards;
public Manager(int employeeNumber, String employeeName, double employeeSalary) {
super(employeeNumber, employeeName, employeeSalary);
}
public void display() {
super.display();
System.out.println(rewards);
}
}
Tester:
public class Test {
public static void main(String [] args) {
Manager manager = new Manager(11111, "Elon Musk", 42344);
manager.display();
Employee employeeOne = new Employee(324, "Bob Den", 3522);
Employee employeeTwo = new Employee(44, "Tim Pipe", 4234 );
Employee employeeThree = new Employee(42, "Asif Blar", 4321);
private void reviseSalary() {
double employeeSalary = manager.getEmployeeSalary();
manager.setEmployeeSalary(employeeSalary +(employeeSalary /10));
manager.display();
}
}
}
**My issue:
I am getting errors on my test class. When I create a manager object, it says the constructor is undefined. Also, for my private void "reviseSalary", it says I cannot use void
Can anyone tell me what I'm doing wrong, and help in creating my reviseSalary function if possible
Thanks
Just put your reviseSalary out of main method.
This error happens because Java does not support nested function.
public class Test {
public static void main(String [] args) {
Manager manager = new Manager(11111, "Elon Musk", 42344);
manager.display();
Employee employeeOne = new Employee(324, "Bob Den", 3522);
Employee employeeTwo = new Employee(44, "Tim Pipe", 4234 );
Employee employeeThree = new Employee(42, "Asif Blar", 4321);
reviseSalary(manager);
}
private static void reviseSalary(Manager manager) {
double employeeSalary = manager.getEmployeeSalary();
manager.setEmployeeSalary(employeeSalary +(employeeSalary /10));
manager.display();
}
}
This will be your output:
Employee Number: 11111
Employee Name: Elon Musk
Employee Salary: 42344.0
Employee Number: 11111
Employee Name: Elon Musk
Employee Salary: 46578.4
I see two main problems with your code:
1) You declare method reviseSalary() inside main() method. Java doesn't allow you to declare method inside of the other method.
You should rather declare it outside of the main() method and add a keyword static to let you call this method without the need of having an instance of your Test class. Otherwise, if you don't declare this method as static, you would need an instance of Test class and have to call reviseSalary() on this instance like this:
Test t = new Test() ;
t.reviseSalary();
2) You declared this field in the Manager class: private int rewards, but you don't declare any setter method to assign any value to that field.

Illegal Self Reference in creation of class

I am pretty new to Java, my code is giving me the error "Illegal Self reference" in the lines below on Charmander/Squirtle/Bulbasaur.moveList
static Pokemon Charmander = new Pokemon("Fire", "Charmander", 25, Charmander.moveList);
static Pokemon Squirtle = new Pokemon("Water", "Squirtle", 25, Squirtle.moveList);
static Pokemon Bulbasaur = new Pokemon("Grass", "Bulbasaur ", 25, Bulbasaur.moveList);
Here is my code
public class Pokemon_Builder {
public static void main(String[] args) {
Move_Builder mb = new Move_Builder();
Charmander.moveList.add(mb.Ember);
Charmander.moveList.add(mb.Scratch);
Charmander.moveList.add(mb.Willowisp);
Charmander.moveList.add(mb.Recover);
Squirtle.moveList.add(mb.Bubble);
Squirtle.moveList.add(mb.Tackle);
Squirtle.moveList.add(mb.Powdersnow);
Squirtle.moveList.add(mb.Recover);
Bulbasaur.moveList.add(mb.Vinewhip);
Bulbasaur.moveList.add(mb.Poisonpowder);
Bulbasaur.moveList.add(mb.Tackle);
Bulbasaur.moveList.add(mb.Recover);
System.out.println(Charmander.moveList.size());
}
static Pokemon Charmander = new Pokemon("Fire", "Charmander", 25, Charmander.moveList);
static Pokemon Squirtle = new Pokemon("Water", "Squirtle", 25, Squirtle.moveList);
static Pokemon Bulbasaur = new Pokemon("Grass", "Bulbasaur ", 25, Bulbasaur.moveList);
}
And here is the code for the Pokemon Class:
import java.util.LinkedList;
import java.util.List;
public class Pokemon{
String type;
String name;
int health;
List<Move> moveList = new LinkedList<Move>();
public Pokemon(String type, String name, int health, LinkedList moveList) {
this.type = type;
this.name = name;
this.health = health;
this.moveList = moveList;
}
public void getInfo (){
System.out.println("Pokemon Name "+ this.name);
System.out.println("Your Pokemon's type "+ this.type);
System.out.println("Your Pokemon's health "+ this.health);
}
public void addMove(Move toAdd){
if (moveList.size() < 5){
moveList.add(toAdd);
}
else{System.out.println("Can't learn any more moves!");
}
}
}
Thanks in advance for the help
In your class Pokemon_builder you create 3 pokemon, while creating those pokemon you provide a moveList. Those movelists are created when the pokemon is created. Meaning what you now try to do is pass a field of a pokemon to the constructor for that pokemon.
As your Pokemon object is instantiating itself the the static member object creation runs before the constructors do. You are trying to create an object called Charmander and pass into Charmander's constructor a reference to a static "movelist" object within Charmander. You are doing this BEFORE Charmander has completed its object instantiation process. Thus you are trying to create an object that needs a reference within itself to "construct" itself.

ArrayList and connection different classes

Im pretty new to Java. I'm trying to connect these classes together. The Go class, is the main class, that should end up running the program. According to Eclipse, the program doesn't contain any errors, but while running, the outprint is blank.
The Go class:
public class Go {
public static void main(String[] args) {
Data klasseObject = new Data();
klasseObject.infoListe();
}
}
The Ansat class:
import java.util.ArrayList;
public class Ansat {
public String navn;
public int alder;
public Ansat(String navn, int alder, ArrayList<Ansat> ansat){
this.navn = navn;
this.alder = alder;
}
public int getAlder() {
return alder;
}
public void setAlder(int alder) {
this.alder = alder;
}
public String getNavn() {
return navn;
}
public void setNavn(String navn) {
this.navn = navn;
}
}
The Data class:
import java.util.ArrayList;
public class Data {
private ArrayList<Ansat> ansat;
public void infoListe(){
ansat = new ArrayList<Ansat>();
ansat.add(new Ansat("Hej", 123, ansat));
}
public ArrayList<Ansat> getAnsat() {
return ansat;
}
}
Output the contents of ArrayList to console
public class Go {
public static void main(String[] args) {
Data klasseObject = new Data();
klasseObject.infoListe();
for(Ansat ansat : getAnsat()){
system.out.println(ansat.getNavn(), ansat.getAlder());
}
}
}
I recommend just two modifications for you to get a proper readable output.
Add the following method to your Ansat class
//modify the returned string however you want it to appear
public String toString() {
return navn + " , " + alder;
}
and then add this line in your main method of Go class (last statement)
System.out.println(klasseObject.getAnsat().get(0).toString());
The toString() class that is added to the Ansat is overriding the toString() method for Ansat meaning that it allows you to print the fields of Ansat class the way you want it and whenever you invoke toString() on object of Ansat then it will pretty print it for you such as below:
Hej , 123
You can update the toString() method to print it however you want.
If you wish to have more than one element in your ArrayList then you have to do the following changes (but, I do want state that you are not doing this the right way):
Data klasseObject = new Data();
klasseObject.infoListe();
Data klasseObject2 = new Data();
klasseObject.infoListe();
Data klasseObject3 = new Data();
klasseObject.infoListe();
for(Ansat s: klasseObject.getAnsat())
System.out.println(s.toString());
And this changes to your Data class
public void infoListe(){
if(ansat != null) {
ansat.add(new Ansat("Hej", 123, ansat));
} else {
ansat = new ArrayList<Ansat>();
ansat.add(new Ansat("Hej", 123, ansat));
}
}
If I were to review your code and suggest improvements, then I would do the following changes in your classes (copy/paste the following code Go.java file and run it):
import java.util.ArrayList;
public class Go {
public static void main(String[] args) {
// running below creates an ArrayList<Ansat> that is inside KlasseObject
Data klasseObject = new Data();
// creates one Ansat(Hey,123) and add it to list
klasseObject.setData("Hey", 123);
// creates one Ansat(Raf,555) and add it to list
klasseObject.setData("Raf", 555);
// creates one Ansat(X-men,999) and add it to list
klasseObject.setData("X-men", 999);
//as many classes as you want, it would add them all to the list
//of klasseObject
// now that we set three Ansats, we will retrieve the list and print
// them all
for (Ansat s : klasseObject.getAnsatList())
System.out.println(s.toString());
}
}
class Ansat {
public String navn;
public int alder;
//remove the array list from constructor, not needed
public Ansat(String navn, int alder) {
this.navn = navn;
this.alder = alder;
}
public int getAlder() {
return alder;
}
public void setAlder(int alder) {
this.alder = alder;
}
public String getNavn() {
return navn;
}
public void setNavn(String navn) {
this.navn = navn;
}
//overrided toString method to pretty-print Ansat object
public String toString() {
return navn + " , " + alder;
}
}
class Data {
private ArrayList<Ansat> ansat;
// added the constructor for Data to initialize Data with empty list
public Data() {
ansat = new ArrayList<Ansat>();
}
//replaced infoListe to setData and added args to it so you can
//pass them from main method
public void setData(String name, int age) {
// every time setData is called a new Ansat is added to list
Ansat a = new Ansat(name, age);
ansat.add(a);
}
public ArrayList<Ansat> getAnsatList() {
return ansat;
}
}
Actually the process what you have followed is perfectly correct,But your getting blank because your not printing the arraylist, hence your getting blank output. Just add the below line and you will see the correct output.
public void infoListe(){
ansat = new ArrayList<Ansat>();
ansat.add(new Ansat("Hej", 123, ansat));
System.out.println(ansat);
}
or in the main function just use it like this...
public static void main(String[] args) {
Data klasseObject = new Data();
klasseObject.infoListe();
System.out.println(klasseObject.getAnsat());
}
Even iterating over array list will fetch you the output -
for (Ansat ansatLoop : klasseObject.getAnsat()) {
System.out.println(ansatLoop.getAlder() + ":"
+ ansatLoop.getNavn());
}
I hope this would solve your query.
Your code is working Perfectly! It has no
System.out.println();
anywhere in the methods that run.
If you modify the method infoListe() to add a println it will print something out
public void infoListe(){
ansat = new ArrayList<Ansat>();
ansat.add(new Ansat("Hej", 123, ansat));
System.out.println("Element Added to ArrayList");
}

Instantiating and Updating Several Objects

Currently I am teaching myself Java but I came across a simple problem but have no one to ask from. From one of the exercises, I wrote a class and write a driver class that instantiates and updates several objects. I am confused by "instantiates and updates several objects." Here is what I mean: So here is my class:
public class PP43Car {
private String make = "";
private String model = "";
private int year;
public PP43Car(String ma, String m, int y)
{
make = ma;
model = m;
year = y;
}
public void setMake(String ma)
{
make = ma;
}
public String getMake()
{
return make;
}
public void setModel(String m)
{
model = m;
}
public String getModel()
{
return model;
}
public void setYear(int y)
{
year = y;
}
public int getYear()
{
return year;
}
public String toString()
{
String result = "Make of the vehicle: " + make +
" Model of the vehicle " + model +
" Year of the vehicle: " + year;
return result;
}
}
Which instantiates make, model and year. Then once I was writing the driver class, the way I began was:
import java.util.Scanner;
public class PP43CarTest {
public static void main(String[] args) {
PP43Car car1;
Scanner scan = new Scanner(System.in);
System.out.println("Enter the model of the vehicle:");
car1.getModel();
}
}
But this class produces error and here is where I am stuck. Do I keep on going with this or is this what is meant by "instantiating and updating several objects?"
import java.util.Scanner;
public class PP43CarTest {
static PP43Car car1;
public static void main(String[] args) {
//Scanner scan = new Scanner(System.in);
car1 = new PP43Car("Millenia", "Mazda", 2011);
}
}
If the above code is correct, then can anyone show me how I can use the Scanner class to actually get the user input and update it that way because I would like to learn that as well?
Well, in your last fragment of code you are indeed instantiating an object, since you are doing:
car1 = new PP43Car("Millenia", "Mazda", 2011);
When you create a new object, you are creating a new instance of the class, so yes, you are instantiaing an object.
But you aren't updating it anywhere, because I think here updating means modifying the object, but you only create the object, not modify it...
Something like this would be an update:
car1.setYear(2013);
Since you are setting a different value for an attribute of the object, you are updating it...
EDIT: Try this code, it can't throw any exception, it's Java basics! I hope it clarifies your doubts...
public class PP43CarTest {
public static void main(String[] args) {
//Declaring objects
PP43Car car1;
PP43Car car2;
PP43Car car3;
//Instantiating objects
car1 = new PP43Car("Millenia", "Mazda", 2011);
car2 = new PP43Car("Aaaa", "Bbb", 2012);
car3 = new PP43Car("Ccc", "Ddd", 2012);
//Updating objects
car1.setMake("Xxx");
car1.setMake("Yyy");
car1.setYear(2013);
//Printing objects
System.out.println("CAR 1: " + car1.toString());
System.out.println("CAR 2: " + car2.toString());
System.out.println("CAR 3: " + car3.toString());
}
}

Categories