Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
129 views
in Technique[技术] by (71.8m points)

java - Guidance on creating an instance for a class with boolean

I'm currently trying to create an instance from a different file and the class has a boolean. I've tried calling it a couple different ways, but I'm unable to get the results I'm seeking. This is how the class starts below:

 public boolean buyFunction(int balance, double amountSpent)

Here's one of the instances I've tried creating

boolean buyFunction buy = buyFunction();
        

I just get the error message

error: ';' expected
        boolean buyFunction buy = buyFunction();
question from:https://stackoverflow.com/questions/65909220/guidance-on-creating-an-instance-for-a-class-with-boolean

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Firstly, buyFunction(int balance, double amountSpent) is a method, not a class. Secondly:

boolean buyFunction buy = buyFunction();

Looks like it has two names, which a variable can't have. Also, when you called the method, you did not pass in an int, and a double. Also, to call the method without an instance of a class, you'd have to instantiate the object.

Here is something that could work:

YourClassName y = new YourClassName();

boolean buy = y.buyFunction(500, 45.50);

Now if you don't want to make an object, and just call the function, you could make use the static keyword:

 public static boolean buyFunction(int balance, double amountSpent)

Now you can call it like this:

boolean buy = buyFunction(500, 45.50);

This would pass 500 to the balance parameter, and 45.50 to the amountSpent parameter.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...