Re: Hi guys, can any one tell what's wrong with my code?This is about stack as arrays and this is based on bluej environment

Well,
this is a stack as an array program.When i call the push function for the first time, the 'max' value gets changed to zero, even though its a global variable, and i accept its value from the user. Any suggestion?

December 27th, 2013, 11:36 AM

Norm

Re: Hi guys, can any one tell what's wrong with my code?This is about stack as arrays and this is based on bluej environment

Can you copy the program's output that shows what the problem is and paste it here?

Quote:

'max' value gets changed to zero, even though its a global variable

Being global does not prevent a variable's value from changing. If you don't want a variable's value to change make it final.
The default value for an int variable is 0. max will start with a value of 0.

December 27th, 2013, 11:48 AM

Kartik V

Re: Hi guys, can any one tell what's wrong with my code?This is about stack as arrays and this is based on bluej environment

how do i make it final? i have to accept the max value in the main method, and after doing dat how shall i make it final? Btw, can u tell me why does the global variable's value change in the other method?

December 27th, 2013, 01:19 PM

Norm

Re: Hi guys, can any one tell what's wrong with my code?This is about stack as arrays and this is based on bluej environment

Quote:

how do i make it final?

Use the final keyword when the variable is defined:

Code :

final int max = 1234;

Quote:

why does the global variable's value change

Where is the variable named: global defined? I don't see any variables with that name in the code.
What variable are you asking about? Why do you call them: global?

December 28th, 2013, 01:04 AM

Kartik V

2 Attachment(s)

Re: Hi guys, can any one tell what's wrong with my code?This is about stack as arrays and this is based on bluej environment

Attachment 2572
Sorry for not being clear.The 'global' variable i was referring to was 'max'. If you see the output snapshot, when i call the Push() method, max's value gets changed to zero, even though i accepted it's value from user(6 in this case). I wanna know why that is happening. :)

December 28th, 2013, 05:48 AM

Norm

Re: Hi guys, can any one tell what's wrong with my code?This is about stack as arrays and this is based on bluej environment

Quote:

when i call the Push() method, max's value gets changed to zero,

Can you copy the full contents of the command prompt window that shows what you are saying and pasted it here? No images because text can not be copied from images when making a response.

December 29th, 2013, 11:34 PM

KotiChowdary

Re: Hi guys, can any one tell what's wrong with my code?This is about stack as arrays and this is based on bluej environment