• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Invoking methods within the same class and from different classes

 
Greenhorn
Posts: 8
Netbeans IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello MS,

First I would like to say I'm really happy I joined this forums, it's gonna help me a lot during my study of Java. I just started with Java and I think I got most of the basics, I know what classes are, objects, instances, methods, arguments, etc. And I also know a little about some of the functions in java such as, for & while loops, if statement, switch statement, using some pre-made classes such as JOptionPane and Scanner and some other stuff.

Anyhow, currently I'm having hard time understanding how to invoke a method correctly, either within the same class or from a different class, and what is the difference between these tow processes ?
Let's say I have tow classes, Class1 & Class2 as the following :




And



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 ?
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 ?

Sorry if some of my questions are stupid or what so ever, I'm on the beginning of the road, so bare with me : )
Any help is appreciated guys, so thanks in advance .







RegardS,
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ?


Looks fine to me. Have you compiled it? that'll soon point up any errors.

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).

should it be 1 new object per class or 1 new object per call for a method ?


Neither.

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 ).

is an example of a method with a parameter, but I'm not sure how it helps you.

4- What do you think about my code ? is it up to Java standard ? clean and easy to read ?


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.

5- Any other notes, observations or advice ?


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
Greenhorn
Posts: 8
Netbeans IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:
Looks fine to me. Have you compiled it? that'll soon point up any errors.


Yes I did, and it worked just fine, I was just wondering if there is any other (better) way.

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).


I see, well this brings me to another question now, what is the difference between static and non-static methods ? and when exactly do I need to use either this or that ?

Winston Gutkowski wrote:
is an example of a method with a parameter, but I'm not sure how it helps you.


Well, I thought if I saw an example it might help me understand, but I'm still confused a little, and I need to dig deeper.

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



Alrighty then, thank you so much Winston you've helped me a lot, and trust me I'm gonna enjoy every little bit of this




Thanks again,
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Adnan Al-Jehani
Greenhorn
Posts: 8
Netbeans IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



Thanks Martin,
And yeah I was about to do that, then I got the assigned book for the class and it was like +900 pages, that's where I got kinda lost between resources and decided to go with some online lectures from Stanford University and some other Youtube channels xD !
Thanks for the tip though, I'm gonna use these tutorials as a resource sooner or later .



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.



Well I gotta say, this is a great example. In my head I know what I meant when I asked the question, but I guess you're right it's not very clear to you guys and the code I give probably didn't help very much !
Anyways, I got what you mean, and hopefully as I advance in Java I'll figure this out by myself, and if I ever faced any problems I'll come to you guys ;).

Thanks for your valuable input Paul, I really appreciate it.



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.



Well, to be honest I didn't fully understand what you meant, but I guess you're going in the same direction as Paul ? Thanks anyways Jeff
reply
    Bookmark Topic Watch Topic
  • New Topic