wood burning stoves 2.0*
The moose likes Java in General and the fly likes getter and setters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "getter and setters" Watch "getter and setters" New topic
Author

getter and setters

Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
Hello,

Unable to get start and end parameter


OUTPUT:
========
Feb 27, 2012 1:52:46 PM org.apache.catalina.core.StandardContext reload
INFO: Reloading this Context has started
getStart=>0
setStart=>1
26
0
start==>0
end==>0

Where I am making mistake?

Thanks in anticipation


Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4375
    
    8

It's difficult to be sure, because you've missed out some crucual bits. Where are you calling setStart, for instance? But it looks like you're getting the value of start, which is initially zero, setting it to something different, and then just printing out the original value.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Rather than guessing at what's relevant and posting partial code, it's far more effective to produce an SSCCE(⇐click) that demonstrates the problem and that anybody can copy, paste, compile, and run. I don't see where you create in instance of your object, or where you call setStart.

Also, your non-standard, seemingly random indentation makes it very hard to read your code. And Java class names conventionally start with a capital letter.

I have a couple of guesses as to what the problem might be, but I'd rather see enough code to not have to guess.
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
Matthew Brown wrote:It's difficult to be sure, because you've missed out some crucual bits. Where are you calling setStart, for instance? But it looks like you're getting the value of start, which is initially zero, setting it to something different, and then just printing out the original value.


Thanks Matthew Brown for your favorable reply.

I initialized int start = 0; but putting value of setStart(int starts) into it and then getting but its reading the value that I instantiated rather then value of setStart method. How can I correct it?

thanks again & best regards
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4375
    
    8

Farakh khan wrote:
I initialized int start = 0; but putting value of setStart(int starts) into it and then getting but its reading the value that I instantiated rather then value of setStart method. How can I correct it?

Well, since you still haven't shown us where you call setStart it's very hard to say. But your output says that you're calling getStart before setStart. If you don't call getStart again afterwards you aren't trying to read the updated value.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Farakh khan wrote:
I initialized int start = 0; but putting value of setStart(int starts) into it and then getting but its reading the value that I instantiated rather then value of setStart method. How can I correct it?


By fixing the bug in the code that you're not showing us, and that we've both told you we need to see.

If it's giving you a 0 after you think you put another value into it, say X, then it could be any of the following:

1. You put X into a different object's variable than the one you're printing.

2. You set it back to 0 after setting it to X.

3. You never actually set it to X in the first place.

I know you think that you didn't do any of those things, but the fact that your code isn't doing what you expected it to means that you have to check your assumptions, since clearly at least one of them is wrong.
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
Thanks all for favorable replies. Here is my code:


Thanks again
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

That's a little better, but since it's still far from an SSCCE, and still hard to read due to messed up indenting, I can't say for sure what the problem is.

It appears to be:
1. You put X into a different object's variable than the one you're printing.

I can see where you're calling setStart(), and the output of 26 supports that. So it looks like you have two different JobMethods objects. Can't be certain though.
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
I am calling in other method as:



Thanks again & best regards
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
I tried to overload constructor instead of getter and setter as:


Its throwing following error:


Thanks again
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

Servlets must have a constructor that takes no parameters. Any configuration is instead done in the init method. In your case the start and end would come from the init parameters which you can access using the ServletConfig object passed to the init method.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Farakh khan wrote:I tried to overload constructor instead of getter and setter as:
[code]
//JobMethod class
ManageJobs mj = new ManageJobs(start,end);


public class ManageJobs extends HttpServlet{

int start=0, end=0;


HUGE red flag right there. Servlets should not have member variables. Ever.
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732


The actual problem is that I want to get start and end parameter from a method pagping of class JobMethods to ManageJobs class and then to pass them into other method of JobDAO class.

Thanks for favorable replies
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

Jeff Verdegan wrote:HUGE red flag right there. Servlets should not have member variables. Ever.

I think you want to tone that statement down. Servlets can have member variables, as long as you make sure they are not modified from doGet, doPost etc. Changing (setting) them from the init method should be fine; otherwise, how could you tweak the servlet using init parameters?

Farakh khan wrote:The actual problem is that I want to get start and end parameter from a method pagping of class JobMethods to ManageJobs class and then to pass them into other method of JobDAO class.

Then you should use (request / session) attributes for those, not member variables. These make your servlet thread-unsafe, and that's something you definitely don't want.
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
Thanks Rob for favorable replies



It start working but not accurately. sometimes it displaying correct and sometime incorrect.
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
Okay,



Thanks again
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Rob Spoor wrote:
Jeff Verdegan wrote:HUGE red flag right there. Servlets should not have member variables. Ever.

I think you want to tone that statement down.


Nope.

Servlets can have member variables, as long as you make sure they are not modified from doGet, doPost etc. Changing (setting) them from the init method should be fine; otherwise, how could you tweak the servlet using init parameters?


Though I generally don't go in for absolutes, this is one of those handful of cases where I do prefer to tell beginners "never", under the assumption that when they're experienced enough to need an exception to the rule and to properly employ it, they'll recognize it and amend their rulebook accordingly.
 
Don't get me started about those stupid light bulbs.
 
subject: getter and setters