Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getter and setters

 
Farakh khan
Ranch Hand
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all for favorable replies. Here is my code:


Thanks again
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am calling in other method as:



Thanks again & best regards
 
Farakh khan
Ranch Hand
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to overload constructor instead of getter and setter as:


Its throwing following error:


Thanks again
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob for favorable replies



It start working but not accurately. sometimes it displaying correct and sometime incorrect.
 
Farakh khan
Ranch Hand
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay,



Thanks again
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic