• Post Reply Bookmark Topic Watch Topic
  • New Topic

Method Parameters  RSS feed

 
Mezan Shareef
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends, how are you all, hope every one is hale and healthy. I have few doubts, which i never cleared.

I am always confused about method parameters. I am not confident of anything about java i know, i know lot of java, but never practiced much. I want to clearly know these things

1. when should i pass parameters to methods.
2. how many parameters should i pass.
3. How do i determine, how many to pass.
4.how to create an object with constructors without using new.

I know how to write a class, how to create objects using 'new'. how to call methods, i know what are class variables, instance variables, how to call them, when to call, where to call.
but i get confused with passing parameters because, i just do not understand. I am stuck like anything. How much ever i try to move forward, i am not able to get sense of learning, because of this parameter thing.

Please shower some light on my head, and explain me. I googled, i read few books, everything done, nothing goes inside my mind, became saturated. I hate method parameters. I can write many methods with out parameters. But i can not stay at that level, i want to learn more and understand more.

Please for God sake explain me very breiefly about this parameter irritation.
 
Max White
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mezan.
One needs to pass parameters when the method is expecting it.The same is applicable for their count.If a method is defined with 2 parameters,you can pass just 2,neither less nor more.
I hope I got your question correctly.
 
Mezan Shareef
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you soo much for replying max,my doubt is how do i define a method with parameters, to my knowledge i do not completely understand when should i define a method with parameters, when should not define with parameters. I have no idea.

for example, people write programs lets say addition of two numbers. with out using any parameters, also some times using parameters. most of the time parameters are used in bigger projects where it is easy to read programs. But i just dont, understand, defining method with parameters....
 
Max White
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you get the code of the method adding 2 numbers?
 
Mykhailo Kozik
Greenhorn
Posts: 16
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general, when you create method you should be aware about context of your application, also maybe some architectural question.

For example you want to add two numbers

Simple way to use static method in some class.


But the problem is when another method wants to do some actions with result.
Thats why you must get result, and pass it to another method.
Imagine if there are lot of such methods, and your callings will be look like:

and so on.

But you can define class with responsibility of all these operations.
You need only one holder for this simple example:

As you see this code pretty bigger than previous one, but it more understandable.
Perhaps, you must see which approach is better for your program structure.

P.S. As i know, you can't create object without keyword new. Maybe it possible via reflection API.
P.P.S. Good book about art of programming that describes similar problems Bob Martin - Clean Code
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!