[Logo]
Forums Register Login
problem on: passing parameter that it only reads as null for every value
mainClass.java


operation.java



Output:
random: 2
random: 4
numThread: 0
totalLength: 0
object: 2
object: 4

Questions:
1- Why the numThread and totalLength prints 0 seems like a null value and not the object?
2- What should i do to make this work? solution for numThread and totalLength.
Hello Sara Brown,

Welcome to CodeRanch!

Please post the complete code so that it would be easy to understand. e.g.
1) I do not see any main method, so I'm assuming that the code given under mainClass.java is inside main method.
2) What is '2' in first for loop of mainClass? Please avoid using magic numbers (or at least give them a meaningful variable names)
3) What is 'size'? Whose size it is?

Anyway, apart from those, lets get to your question - why numThread and Length print 0 (one more thing - please follow naming conventions - class names start with Uppercase letter, and variable names follow camelCaseFormat).

What you are doing here is:

You are taking arguments, and overwriting those by class variables. Since you haven't initialized those (again, this is purely my assumption - because you haven't completely posted the code ), those variables are getting initialized by their default values (Object is null, int is 0 and so on).
So, you are actually assigning 0 and null to input parameters before printing it.

I hope this helps.
sorry..this is my program..please do help.

It does not passing the parameter that i hope and keep read as null only the object works properly.

and return numThread in the operation class which i bold, also passing the null value


*p/s: i bold some of the code that might be error..*


in main class



operation class
 
Sara Brown wrote:only the object works properly

That is because, you are passing an object, and printing it without modifying at all. But as I've mentioned in my previous post, you are modifying other arguments before printing.

Please take a look carefully at my previous post - I've also mentioned the problematic part.
 
Sara Brown wrote:


What Anayonkar is saying (correctly) is that your two assignments are in reverse order: you are assigning the class member TO the local variable, rather than assigning the local variable TO the class member. To prove the point, change the code to this:


By adding final to the input parameter list you prevent yourself from making the mistake you are making. What error do you get? Why?
thank you. it is just my silly mistakes. i am really sorry.
 
Sara Brown wrote:thank you. it is just my silly mistakes. i am really sorry.

You are welcome. It happens I'm glad that you understood the issue.

Besides, I think Steve Luke's latest post contains same issue - which I guess might be a typo (during copy-paste it seems those changes - reversing the assignment order - were missed)

It should be like this:

Moved to the Java in General forum.

Henry
 
Anayonkar Shivalkar wrote:...Besides, I think Steve Luke's latest post contains same issue - which I guess might be a typo ...


No, it was supposed to have the same error to show that putting final in the parameter declaration shows the error at compile time.
 
Steve Luke wrote:
Anayonkar Shivalkar wrote:...Besides, I think Steve Luke's latest post contains same issue - which I guess might be a typo ...


No, it was supposed to have the same error to show that putting final in the parameter declaration shows the error at compile time.

Ah... got it. My mistake
Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards



All times above are in ranch (not your local) time.
The current ranch time is
Dec 14, 2017 18:03:06.