简体   繁体   中英

How can I assign variables to my class constructor if they are static?

I'm running into a problem where I am using a method to take in user data and the method is to return an object. The problem is whenever I get to assigning the inputted values to the class constructor I am met with an error message stating, "non-static variable this cannot be referenced from a static context." I am aware that my methods are declared using public static [return value], which is ultimately leading to this issue. I have to keep the methods as static, though, as a part of this project. That leaves me somehow manipulating the class or class constructor, but I'm not certain as to how to do that.

This is the class and class constructor:

public class Project1 {

    public class Student {
        String name;
        String ID;
        float GPA;
        int creditHours;
        double tuitionCost;

        public static Student (String name, String ID, float GPA, int creditHours, double tuitionCost) {
            this.name = name;
            this.ID = ID;
            this.GPA = GPA;
            this.creditHours = creditHours;
            this.tuitionCost = tuitionCost;
        }
    }

This is how I am attempting to assign the user inputted data to the class constructor.

    Student ret = new Student();

    ret.name = name;
    ret.ID = ID;
    ret.GPA = GPA;
    ret.creditHours = creditHours;
    ret.tuitionCost = tuitionCost;

    return ret;

How can I assign the values I have read in from the user (using Scanner) to the class constructor if the method in which they are read in is static?

Note: I'm new to Java, so some of my jargon may be a bit off.

  • static constructors are not allowed in Java

  • Then your Student class is not static , it means that it could be created only with parent Project1 .

    public class Project1 {

     public class Student { public Student() { } } public static void main(String... args) { Project1 project1 = new Project1(); Student student = project1.new Student(); }

    }

  • You have to make Student class statis if you want to use Student class without Project1 .

    public class Project1 {

     public static class Student { public Student() { } } public static void main(String... args) { Student student = new Project1.Student(); }

    }

The title of the question seems very simple but the description is quite confusing:

How can I assign variables to my class constructor if they are static?

As per my understanding, if you want to assign some static variables in your constructor then in the first place you should have static variables present in your class which seems to be missing.BUT there is something more which you are missing.

There are no static constructors in java, you might need to refer:

Why a constructor cannot be static or Can we define static constructors

I doubt the code snippet you posted would compile because the compiler should throw an error as you can never have a static constructor in a class.

Now coming to your actual issue:

How can I assign the values I have read in from the user (using Scanner) to the class constructor if the method in which they are read in is static?

I modified your code little bit to solve your issue:

package com.sopra.banking.compliance.report.backend.common.bean;

import java.util.Scanner;

public class Test
{
    public static class Student
    {

    String name;

    public Student()
    {

    }

    public Student(String name)
    {
        Student student = readValues();
        this.name = student.name;
    }

    static Student readValues()
    {
        Scanner sc = new Scanner(System.in);
        Student student = new Student();
        String name = sc.next();
        student.name = name;
        return student;
        }
    }
}

As a side note, you were having an inner class and if you want to define a static method in an inner class then you need to make that class as static.

Constructors cannot be static. Methods can.

If you need a static method that returns a Student, it would look like

public static Student buildStudent(String name, String ID, float GPA, int creditHours, double tuitionCost) {
    Student ret = new Student();

    ret.name = name;
    ret.ID = ID;
    ret.GPA = GPA;
    ret.creditHours = creditHours;
    ret.tuitionCost = tuitionCost;

    return ret;
}

However, using an actual constructor is arguably more straightforward for object creation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM