Adnan Al-Jehani wrote:
Ok, my questions are :
1- I'm I calling the methods within the same Class and from the other classes correctly ?
2- Do I have to create a new object every time I'm calling a method ?
should it be 1 new object per class or 1 new object per call for a method ?
is an example of a method with a parameter, but I'm not sure how it helps you.3- How exactly can I involve the parameters (arguments) and make them part of the method ? (if you could include a simple mathematical example it would be great ).
4- What do you think about my code ? is it up to Java standard ? clean and easy to read ?
5- Any other notes, observations or advice ?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
Looks fine to me. Have you compiled it? that'll soon point up any errors.
Winston Gutkowski wrote:No. In fact, if the method is static you don't need to create an object at all (but please don't go making all your methods static because of that).
Winston Gutkowski wrote:
is an example of a method with a parameter, but I'm not sure how it helps you.
Winston Gutkowski wrote:Fine so far, but I think you have a bit further to go before you need to worry about that stuff. Make it correct; worry about the other stuff later.
Yes. Enjoy it. An awful lot of good programs have been created by people who love to "muck about" with bits and bytes.
It's not for everybody, and programming is not easy; so don't worry if some of it goes over your head. If it's for you, you'll find out fairly quickly; if not, there's a pile of other things out there to learn.
Winston
Adnan Al-Jehani wrote:2- Do I have to create a new object every time I'm calling a method ? should it be 1 new object per class or 1 new object per call for a method ?
Winston Gutkowski wrote:
Adnan Al-Jehani wrote:
2- Do I have to create a new object every time I'm calling a method ?
No. In fact, if the method is static you don't need to create an object at all (but please don't go making all your methods static because of that).
Martin Vajsar wrote:Welcome to the Ranch, Adnan!
I'd also suggest to read Oracle's (formely Sun's) tutorials - they are excellent and they will help you a lot to learn Java. You can start here.
Paul Clapham wrote:
Adnan Al-Jehani wrote:2- Do I have to create a new object every time I'm calling a method ? should it be 1 new object per class or 1 new object per call for a method ?
In your example? It's impossible to say.
If your class was named "String" or "File" then it would be possible to ask whether you needed one or two or ten "String" or "File" objects; in fact when you use Strings or Files you don't have any trouble figuring out whether to create a new String or a new File to serve some purpose or other. But when should you create a new "Class1" object? Of course that would depend on what a "Class1" object is supposed to be for. And since it isn't for anything at all in your example (except to hold sample code) then there is never any reason to create any "Class1" objects at all. You need a real example to ask this sort of question.
So let's have a real example, drawn from real life rather than programming. You have 100 workers who are going to a job site, where they will move a huge pile of dirt 100 metres to the left. Now, how many shovel objects do you need for this project? Should each worker have his own shovel, or can they get by with just one shovel?
Okay, you got the right answer for that. Now you can't just send those workers out by themselves, they need supervision to make sure they move the dirt to the right place. So how many supervisor objects do you need? Should each worker have his own supervisor, or can they get by with just one supervisor?
I expect you got the right answer for that, and it wasn't the same answer as the first one. That's because you understood the objects involved in the design. And so it should be in programming; if you understand the classes in your design and what they are for, you will be able to answer those questions.
Jeff Verdegan wrote:
Winston Gutkowski wrote:
Adnan Al-Jehani wrote:
2- Do I have to create a new object every time I'm calling a method ?
No. In fact, if the method is static you don't need to create an object at all (but please don't go making all your methods static because of that).
In fact, you shouldn't even be thinking in terms of "I want to call a method, do I need to create an object?" If you think in terms of, "I need to accomplish some task, and I know that such-and-such object can perform that task, or help in performing it, so I'm going to get me one of those and use it to do this work for me."
Kind of like you don't think, "I want to press a gas pedal and turn a steering wheel, so I'm going to get a car." Rather, you think, "I want to get from point A to point B, and a car can help me do that."
The car (object) is the tool that will help you accomplish your task. The gas pedal and steering wheel are just the particular mechanisms (methods) you use to tell the car (object) how to accomplish your task.
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |