-3

There's an error in my code on the last line of functioning code.

package Class;

import java.util.Scanner;

public class Bicycle 
{    
    public int units;
    public int courseNum;
    public String courseName;

    public Bicycle(int startUnits, int startNum, String startName) 
    {
        units = startUnits;
        courseNum = startNum;
        courseName = startName;
    }

    public int setUnits(int newValue) 
    {
        units = newValue;
        return units;
    }

    public int setNum(int newValue)
    {
        courseNum = newValue;
        return courseNum;
    }

    public String setName(String newValue) 
    {
        courseName = newValue;
        return courseName;
    }

    public class subClass extends Bicycle 
    {
        public int randVariable;

        public subClass(int startUnits, int startNum, String startName) 
        {
            super(startUnits, startNum, startName);
        }   

        public void randVariable(int newValue) 
        {
            randVariable = newValue;
        }   
    }

    public static void main(String args[]) 
    {
        int BaseUnits;
        int BaseCourseNum;
        String BaseCourseName;

        int FinalUnits;
        int FinalcourseNum;
        String FinalcourseName;

        Scanner entries = new Scanner(System.in);

        BaseUnits = entries.nextInt();
        BaseCourseNum = entries.nextInt();
        BaseCourseName = entries.nextLine();

        FinalUnits = setUnits(BaseUnits);
    }
}

The error states that I cannot reference a non-static method from a static context.

So I then proceeded to look up questions on Stack about static and non-static methods, but the answers were all pretty confusing - possibly due to the fact that I am a beginner in programming.

CoolKat
  • 112
  • 1
  • 12
  • What is the error you're getting? –  Dec 08 '14 at 00:13
  • 1
    He stated the error... "The error states that I cannot reference a non-static method from a static context." – VMoza Dec 08 '14 at 00:14
  • 2
    Read about the `static` modifier in a basic Java tutorial. – Boris the Spider Dec 08 '14 at 00:16
  • The only valid answer so far is from @BoristheSpider... read about static, understand it and you will be able to fix the issue yourself! – WarrenFaith Dec 08 '14 at 00:17
  • You're going to want to look up what "static" and "instance" members are. `main()` is static, which means it can be accessed outside the instance of any particular `Bicycle` object. `setUnits()` is an instance method, it requires an instance of a `Bicycle` object. You don't have an instance of a `Bicycle` object. – David Dec 08 '14 at 00:17
  • possible duplicate of [What does the 'static' keyword do in a class?](http://stackoverflow.com/questions/413898/what-does-the-static-keyword-do-in-a-class) – WarrenFaith Dec 08 '14 at 00:24
  • How is this a duplicate of that question? We are dealing with the same concepts, but honestly 1. There's no point in marking this a duplicate and 2. It's not the same exact question – CoolKat Dec 08 '14 at 00:28
  • 2
    @CoolKat the problem is that you don't understand the concept of `static`. If you would understand the concept, you could answer your own questions within a second! And I consider this to be a duplicate. Same error and the answer is a really good one. – WarrenFaith Dec 08 '14 at 00:33

2 Answers2

0

You are Accessing non-static method in a static method.

see your public int setUnits(int newValue); this not static method . you cannot access it in a static method like your main public static void main(String[] args);

you can access it through creating a new instance of that class

This one may/should fix your problem.

Create a new instance of that class on your main method :)

  Bicycle bike = new Bicycle();
  bike.setUnits(someValue); //this is your setUnits method.

or make your setUnit a static method.

  public static int setUnits(int someValue);

by declaring any method or instance variable a static it means it is a class level or owned by that class.

try to look at this. or

this one :)

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
Secondo
  • 451
  • 4
  • 9
  • a static setUnit() will fail as the member variable units is not static and probably should not be static. – WarrenFaith Dec 08 '14 at 00:18
  • @WarrenFaith thats why i have two options its up to him what will he use :) – Secondo Dec 08 '14 at 00:20
  • he dont have any choice tho. he need to create a new instance of Bicycle Class :) – Secondo Dec 08 '14 at 00:21
  • yes, but not making setUnits() static as this changes the behavior of the class... – WarrenFaith Dec 08 '14 at 00:22
  • I edited what you said: public static int setUnits(int newValue) { units = newValue; return units; } But this returns a bunch of errors – CoolKat Dec 08 '14 at 00:23
  • making your setUnits as a static it will change your all workflow just what @WarrenFaith said awhile ago. just create a new Object of Bicycle Class. – Secondo Dec 08 '14 at 00:25
  • and just what i had said when you try to access a non-static either instance variable or method to a static instance variable or method it will cause an error of `cannot reference a non-static method from a static context.` so you should always be aware of this kind of wrongness XD :)) :P – Secondo Dec 08 '14 at 00:27
-1

Just make your setUnits method static, as well as your units variable.

jfdoming
  • 975
  • 10
  • 25