![]() |
retaining values in methods
Hello,
I'm pretty new to OOP and Jave and have ran into an issue on how to code some logic. I have a method that's needing to hold a local variables value upon different calls. I have tried this code but realize that Java can only use static keyword for classes. ( I think i have the code right, my code came off of a different computer that does not have internet access. I think I have remembered it correctly though.) :
private float proc(int a, int b, boolean state){:
private float proc(int a, int b, boolean state){ |
Re: retaining values in methods
You can declare the variable outside the method as a private class variable:
:
private int complete; |
Re: retaining values in methods
:
private float proc(int a, int b, boolean state) {:
private static int complete;And nightFix, the error is because in Java, fields aren't auto initialized like in C++, you need to declare complete as a new Integer object type with the value as the constructor argument; but you should instead just declare it as a primitive 'int'. Primitives to Object types is not an optimization. |
Re: retaining values in methods
Hey,
thank you for the input and quick responses. Null you are right I don't need an else there! Though I don't know if that affects processing time. I was using an Integer object rather than an int declaration because I was told that you can't pass primitive data types by reference nor can you get them to retain their value with different calls unless they are an object. Like I said I'm new to this and am probably needing to review my private vs public uses. Well I'm in a hurry right now but I still haven't got this code working. Hold the posts till I can get some code up later today. I appreciate the replies though and setting me on the right track. Thanks a lot. |
Re: retaining values in methods
Quote:
|
Re: retaining values in methods
OK.
Sorry it took me so long to get back to you all. Well I almost have my code working and believe I am using my access modifiers correctly but have never really used them before so if anyone picks up on a better way to do this than it will be considered. My problem now, is that when my clear() method is called it throws an exception. I've narrowed it down to the my JTextFields you will see identified as txtXXXX. I have a feeling this is probably something simple but for some reason I'm not seeing it. Exception handling with Java is pretty new to me as well. I think I'm posting enough code to make it interpretable. Thanks for bearing with me. :
public class PointOfSale extends JFrame implements ActionListener, ItemListener{ |
Re: retaining values in methods
All right. So it only crashes if you clear while the fields are blank. The way I have it written the input is being tried above the clear actions so obviously if a user presses clear while there are empty text fields this will throw an exception! (you all need an emo face that slaps his head.)
I moved my clear actions above the initiations to keep it from crashing. I think this is proper. :
public void actionPerformed(ActionEvent e){ |
| All times are GMT -5. The time now is 3:51 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC