Jeremy B
Jeremy B

Reputation: 863

instance variable in another class

I'm working on homework and I won't post the full code but I'm stuck on something that's probably simple and I can't find it in my book so I need to be pointed in the right direction. I'm working with classes and interfaces.

Basically in my main code I have a line like this

    CheckingAccount checking = new CheckingAccount(1.0);  // $1 monthly fee

I was told to create a class called CheckingAccount and in that class I am told "This class should include an instance variable for the monthly fee that's initialized to the value that's passed to the constructor.

Since I'm new this is barely english to me and I'm assuming what that is saying is to take that 1.00 fee and declare it in the CheckingAccount class so I can create a method using that variable to calculate something.

soooo... How do I do that? I know how to create an instance variable it would be something like

    public double  monthly fee =

but then what? or I could be wrong. I am really doing bad at this java stuff. Any help is appreciated.

I guess another way to ask it is am I just declaring it as 1.0? or am I "importing" that value in case it changes later at some point you don't have to go through the code to change it in all of the classes?

Upvotes: 0

Views: 4723

Answers (4)

jonathanl
jonathanl

Reputation: 524

@Jeremy:

You're pretty much spot on (at least, your interpretation of what you've been asked to do matches my interpretation); while I don't know the actual design of the class, or whether monthly_fee needs to be public, in pseudocode you'd be looking at something like:

class CheckingAccount {  
    //Instance variable  
    double monthly_fee;    
    //Constructor
    CheckingAccount(double monthly_fee) {  
          this.monthly_fee = monthly_fee;  
    }    
    //Function to multiply instance variable by some multiplier
    //Arguments: Value to multiply the monthly fee by
    double multiply_fee(double a_multiplier) {  
          return monthly_fee*a_multiplier;  
    }  
}

Upvotes: 3

Petr Janeček
Petr Janeček

Reputation: 38414

You are basically right. If you haven't already, you should create a new class (it should be in it's own file called CheckingAccount) like this:

/** This is the class of Account with monthly fee. */
public class CheckingAccount {
    // This is the instance variable.
    // It should be 'private' for reasons you will surely learn soon.
    // And NOT static, since that would be a class variable, not an instance one.
    // The capitalization is called camelCase, google it up. Or, even better, find 'JavaBeans naming conventions'
    private double monthlyFee;

    // This is the constructor. It is called when you create the account.
    // It takes one parameter, the fee, which initializes our instance variable.
    // Keyword 'this' means 'this instance, this object'.
    public CheckingAccount(double monthlyFee) {
        this.monthlyFee = monthlyFee;
    }

    // Here will be your methods to calculate something...
}

Upvotes: 2

Michael Berkowski
Michael Berkowski

Reputation: 270599

Your requirement (as I read it) is to initialize the instance variable in the constructor, and your instantiation (new CheckingAccount(1.0);) shows you are on the right track.

What your class will need is a constructor method which receives and sets that value 1.0.

// Instance var declaration
private double monthly_fee;

// Constructor receives a double as its only param and sets the member variable
public CheckingAccount(double initial_monthly_fee) {
  monthly_fee = inital_monthly_fee;
}

Upvotes: 3

Adam Mihalcin
Adam Mihalcin

Reputation: 14458

Don't create an instance variable as public. It's bad practice because it violates the principle of information hiding (your teacher may call this abstraction). Instead, you can create an instance variable as

public final class CheckingAccount {
    private double monthlyFee;

    // The rest of the class goes here

    public double getMonthlyFee() { // This method is called an accessor for monthlyFee
        return monthlyFee;
    }
}

Note that monthly fee isn't a valid variable name because it contains a space, and variable names can't contain spaces. Also notice that other classes access monthlyFee through a method. Because you define the method rather than making the variable public, you control access to monthlyFee a lot better (another class can't just change monthlyFee unless you define a method that makes that change).

Now to accessing monthlyFee. The method getMonthlyFee is called an accessor for a reason: it allows other classes to access that variable. So, those other classes can just call the method to get the monthly fee out of a CheckingAccount:

CheckingAccount checking = new CheckingAccount(1.0);
// A bunch of other code can go here
double fee = checking.getMonthlyFee(); // Now fee is 1.0

Upvotes: 0

Related Questions