How to use OOP, constructors, and objects to pass variables to methods and main method

how to pass class object as parameter in java
how to call a method with parameters in java
java pass class type as parameter
passing object as parameter in constructor in java
java pass class as parameter
how to call a variable from another method in java
call by value and call by reference in java geeksforgeeks
constructor with object as parameter in java

I have a program in which I am calculating and printing out gas mileage information. The assignment I'm working on wants me to (well I think it wants me to) use OOP, constructor, and object to pass variables from a constructor to my whole program. It makes no sense to me at all.

Here are the exact assignment instructions:

I've tried making my original variables in the main method private static variables and placed them right below my class header. This worked, but I think the instructions want my constructor to include variables somehow. It just doesn't make any sense to me at all. I don't get constructors or objects whatsoever and how to use them in this application, and its frustrating the hell out of me.

CarV3, the program the instructions reference in terms of what I'm supposed to object orient, looked like this:

public class CarV3 {
    public static int calcDistance(int sMiles1, int eMiles1){
        return eMiles1 - sMiles1;

    public static double calcMPG(int dist1, double gals1){
        return dist1 / gals1;

    public static void main(String[]Args) {
        int sMiles1 = 43305;
        int eMiles1 = 43422;
        int dist1;
        double gals1 = 7.8;
        double MPG1;
        String carType1 = "14 Acura MDX";

        CarV3 car1 = new CarV3();

        dist1 = car1.calcDistance(sMiles1, eMiles1);
        MPG1 = car1.calcMPG(dist1, gals1);

        System.out.printf("%52s%n%s%16s%14s%13s%12s%14s%n%s%n", "Gas Mileage Calculations", "Type of Car", "Start Miles", "End Miles", "Distance", "Gallons", "Miles/Gal",
        System.out.printf("%s%15d%14d%13d%12.1f%14.1f", carType1, sMiles1, 
        eMiles1, dist1, gals1, MPG1);

That code worked before. The instructions ask to calculate GPM and other data later, so my program turns into something more like this in CarV5:

public class CarV5 {
    private static int sMiles1 = 43266;
    private static int eMiles1 = 43422;
    private static int dist1;
    private static double gals1 = 7.8;
    private static double MPG1;
    private static double pricePerGallon1 = 2.98;
    private static double GPM1;
    private static double totalCost1;
    private static String carType1 = "14 Acura MDX";

    CarV5() {

    public int calcDistance(int sMiles1, int eMiles1){
        return eMiles1 - sMiles1;

    public static double calcMPG(int dist1, double gals1){
        return dist1 / gals1;

    public static double calcGPM(int dist1, double gals1) {
        return gals1 / dist1;

    public static double totalCost(double pricePerGallon1, double gals) {
        return pricePerGallon1 * gals;

    public static void main(String[]Args) {
        CarV5 car1 = new CarV5();

        dist1 = car1.calcDistance(sMiles1, eMiles1);
        MPG1 = car1.calcMPG(dist1, gals1);
        GPM1 = car1.calcGPM(dist1, gals1);
        totalCost1 = car1.totalCost(pricePerGallon1, gals1);

        System.out.printf("%70s%n%s%16s%14s%13s%12s%10s%9s%14s%13s%n%s%n", "Gas Mileage Calculations", "Type of Car", "Start Miles", "End Miles", "Distance", "Gallons", "Price", "Cost", "Miles/Gal", "Gal/Mile", "================================================================================================================");
        System.out.printf("%s%15d%14d%13d%12.1f%10.2f%9.2f%14.1f%13.3f", carType1, sMiles1, eMiles1, dist1, gals1, pricePerGallon1, totalCost1, MPG1, GPM1);

Although this code works, I'm almost positive it isn't what the instructions asked me to do. I honestly don't understand what the instructions want me to do. The program you see there does print the expected result, which is all of the car data. However, in the instructions, I am told to use some kind of constructor with values in it. If you notice in the second version I have this "CarV5() {}", which I don't understand as to what it does.

The expected output is correct in my second version of the program, CarV5, as mentioned before. A table prints out calculated values of distance, gallons, etc.

My approach is what's wrong. I'm sorry for the super long post, but my friends who are also taking this course think I'm past the point of being taught, and if I'm being honest, maybe I am too. I just really want to figure out how to do this.

Ok, I am not sure what the assignment is as it is not accessible but I think they want you to create a Class with fields which are gonna be set in constructor during object creation. Let me show a brief example:

package tst;

public class CarTest {

    public static void main(String[] args) {
        // creating new instance of the Car and at once setting all its fields in constructor
        // now as I see the assignment, this line corresponds to 
        // CarV5 car1 = new CarV5(carType1, endMiles1, startMiles1, gallonsUsed1, pricePerGallon1);
        Car car = new Car(43266, 43422, 7.8);

        //calculating and printing the result w/o providing values but using instance fields


    // the Car class itself, you can put it in different file
    public static class Car {

        // note that fields are NOT static, i.e. they belong to an INSTANCE of the class, not the class itself
        private int sMiles1;
        private int eMiles1;
        private double gals1;

        public Car(int sMiles1, int eMiles1, double gals1) {
            this.sMiles1 = sMiles1;
            this.eMiles1 = eMiles1;
            this.gals1 = gals1;

        // example of your methods
        public int calcDistance(){
            return this.eMiles1 - this.sMiles1;

        // other methods you need



Let me know if that helps :) PS: I think what confuses you are static vs instance methods, try to refresh this part of the course or use some other source (=google :) )

Java Fundamentals Tutorial: Object Oriented Programming in Java, Even though OOP takes some getting used to, its main benefit is to make it easier to solve real-world problems by modeling The constructor method initializes the state of the new object. Parameters passed into methods are always copied (“pass-by-value”). An example of static method use is Java's Math class. Object-oriented programming (OOP) is a programming paradigm based on “object” which is a way to define something in a programming language containing variable as “field/attributes” and functions as “method”. OOP allows programmers to create their own objects that have methods and attributes. OOP allows a user to create their own

First of all Why is everything static? Second since your link does not work, i can´t really see what is that is being asked to do, so i can only assume, based in what you are saying, that you want to create an object with a constructor, is that it?

public class CarV5 {

private int sMiles1;
private int eMiles1;

public CarV5(int sMiles1Constructor,int eMiles1Constructor){
   this.sMiles1 = sMiles1Constructor;
   this.eMiles1 = eMiles1Constructor;

 your methods...


public static void main(String[]Args) {
    CarV5 car1 = new CarV5(43266,42422);

     //Call your methods here...

basically you just have to pass your variables as arguments in your constructor.

Don't give up man, once you learn this basics it gets easier over time, don't worry.

4. Methods Use Instance Variables: How Objects Behave, Methods Use Instance Variables: How Objects Behave State affects behavior, You can pass variables into a method, as long as the variable type matches the parameter type You're familiar with public –we use it with every main method. A method is the equivalent of a function in object-oriented programming. A noun is to a verb what a variable is to a method — the methods are the actions that perform operations on a variable. A method accepts parameters as arguments, manipulates these, and then produces an output when the method is called on an object. Methods are similar to functions, but methods are also classified

Objects are data types like any other (e.g. int, double, String, ect.) This means that you can create multiple of these Objects with different names and attributes associated with them. The field variables (most commonly in the form of private int field_name; ) are just parts of the value of this Object. You have to see it literally: you directly associate a mile per gallon attribute with any car you read about so this works the same way. Static variables are a simple way of making a value extend that small scope of a class by letting objects that aren't part of the class access variables (only of that certain class of course) that you don't want to be car-specific (e.g. a year variable, which is the same for all objects (unrelated, but a plausible example)). Hope this clears things up!

Java -- Pass variable from class to class, You use getters and setters. And in your main method, wherever it is: You can also use constructors as a means of an "initial setter" so that the object is  In object-oriented programming, a class is a blueprint for creating objects (a particular data structure), providing initial values for state (member variables or attributes), and implementations of behavior (member functions or methods). The user-defined objects are created using the class keyword. The class is a blueprint that defines a nature of a future object. An instance is a specific

Passing Information into a Method, Similar to functions in other programming languages, Java methods accept In Java, you can pass an argument of any valid Java data type into a method. In this example, the method creates a new Polygon object from a list of x , y points Thus using x , y or radius within the body of the constructor refers to the argument ,  A class is a template for creating objects or a collection of objects. Objects have member variables and methods associated with them. In python, a class is created by using keyword class. An object is created using the constructor of the class. This object will then be called the instance of the class. # using OOP – Class example in Python

Class basic syntax, In object-oriented programming, a class is an extensible program-code-template for creating objects, providing initial values for state (member variables) and Then use new MyClass() to create a new object with all the listed methods. The function code is taken from the constructor method (assumed  Clash Royale CLAN TAG #URR8PPP up vote 3 down vote favorite I made a simple program that uses OOP methods. Please tell me if I

Understanding Classes and Objects in Java, It represents the set of properties or methods that are common to all objects of one type. It helps in maintaining the access specifications of member variables. Objects: It is the basic unit of Object Oriented Programming and it represents the real life entities. By using this method, the desired constructor can be called. To create the new instance of the Person object, we first define a variable of type Person that will hold the object. In this example, we've called it dave.; On the other side of the equals sign, we call the constructor method of our Person class and pass it four string values.

  • Your link to assignment is inaccessible: "Sorry! You must log in to view this content."
  • Sorry, I'll fix that, I know why it won't work. Here is a working link:…
  • Thank you for your response. We actually aren't supposed to use more than one class yet. I also don't understand the "this." since we haven't learned that yet. I updated the instructions link so that it should be accessible. I think your approach is really close to what I have to do. Thank you!
  • Yeah, I intentionally split it into two classes so you can better see the class itself (object) and the code which uses the class. You can move main method to class Car, it will work the same way. Regarding this - you can threat at as pointer to class instance, like here this.sMiles1 = sMiles1; we setting constructor parameter sMiles1 to the instance field declared in private int sMiles1;.
  • I think this is a good suggestion. My friend was trying to suggest I put numbers in the declaration for the object. I think I understand what you are doing, but I'm not sure what "this." does. We haven't learned that yet in the course. However, looking at your code I interpret it as I need to feed values to the actual integers through the constructor? I also don't understand the sMiles1Constructor. Is that just a placeholder for something I'd write there? Thank you so much for responding. Also, are you sure the updated Google Drive link doesn't work? I made it "everyone can view". Thanks again
  • sMiles1Constructor is just something i wrote so that you would understand that is the variable passed in the constructor(usually they have the same name), the "this." is referencing the variable that were crated above the constructor, so that you can Access those values later using getters and setters(look that up if you need to), Basically the variables that i created above will have the value passed in the constructor("this." means "this class") . Yes the google drive link is working already.