[program-java] Re: Java: Precedence... solution

  • From: "Corbett, James" <James.Corbett@xxxxxxxxxxxxx>
  • To: "'program-java@xxxxxxxxxxxxx'" <program-java@xxxxxxxxxxxxx>
  • Date: Fri, 17 Sep 2010 15:44:06 -0400

Jim:

Example follows:

Public class MyClass {

Private int x; //member visible to class only

Public MyClass() {// constructor method....
}

Public int getX() { // public accessor method
Return x;
}
Public void setX(int x) { // a public accessor method
This.x =x; //note the use of the key word this. This is used to denote the 
class member x where as the x from the method signature only has scope to the 
method.
}
}
-----Original Message-----
From: program-java-bounce@xxxxxxxxxxxxx 
[mailto:program-java-bounce@xxxxxxxxxxxxx] On Behalf Of Homme, James
Sent: September 17, 2010 15:24
To: program-java@xxxxxxxxxxxxx
Subject: [program-java] Re: Java: Precedence... solution

Hi Jim,
Or maybe you are saying that x was defined somewhere outside the code sample?

Jim

Jim Homme,
Usability Services,
Phone: 412-544-1810. Skype: jim.homme
Internal recipients,  Read my accessibility blog. Discuss accessibility here. 
Accessibility Wiki: Breaking news and accessibility advice


-----Original Message-----
From: program-java-bounce@xxxxxxxxxxxxx 
[mailto:program-java-bounce@xxxxxxxxxxxxx] On Behalf Of Homme, James
Sent: Friday, September 17, 2010 3:22 PM
To: program-java@xxxxxxxxxxxxx
Subject: [program-java] Re: Java: Precedence... solution

Hi Jim,
Are you saying that since it doesn't say for(int x) that doing that makes x 
global? And is x seen outside the class?

Thanks.

Jim

Jim Homme,
Usability Services,
Phone: 412-544-1810. Skype: jim.homme
Internal recipients,  Read my accessibility blog. Discuss accessibility here. 
Accessibility Wiki: Breaking news and accessibility advice


-----Original Message-----
From: program-java-bounce@xxxxxxxxxxxxx 
[mailto:program-java-bounce@xxxxxxxxxxxxx] On Behalf Of Corbett, James
Sent: Friday, September 17, 2010 3:20 PM
To: 'program-java@xxxxxxxxxxxxx'
Subject: [program-java] Re: Java: Precedence... solution

Jim:

Typically getters and setters are of public access because they can modify the 
private members associated with them. My private method MyMethod is used from 
with in a class and is a helper method. Since it returns nor sets anything its 
name usually refers to what it does. In this example I used myMethod as just a 
name space only.

Secondly if you notice that x wasn't declared with in the for loop, the blobal 
x takes over the driver seat.

Jim

-----Original Message-----
From: program-java-bounce@xxxxxxxxxxxxx 
[mailto:program-java-bounce@xxxxxxxxxxxxx] On Behalf Of Homme, James
Sent: September 17, 2010 14:24
To: program-java@xxxxxxxxxxxxx
Subject: [program-java] Re: Java: Precedence... solution

Hi,

OK. I just today learned how to make a getter, a setter, and a constructer. The 
advice the book gives is that most of the time we should make methods public 
and fields private. Why is the myMethod method private?  Second question. It 
looks like the scope of the variable x is only inside the loop. Is that right?



Thanks.



Jim



Jim Homme,

Usability Services,

Phone: 412-544-1810. Skype: jim.homme

Internal recipients,  Read my accessibility blog 
<http://mysites.highmark.com/personal/lidikki/Blog/default.aspx> . Discuss 
accessibility here 
<http://collaborate.highmark.com/COP/technical/accessibility/default.aspx> . 
Accessibility Wiki: Breaking news and accessibility advice 
<http://collaborate.highmark.com/COP/technical/accessibility/Accessibility%20Wiki/Forms/AllPages.aspx>



From: program-java-bounce@xxxxxxxxxxxxx 
[mailto:program-java-bounce@xxxxxxxxxxxxx] On Behalf Of Corbett, James
Sent: Friday, September 17, 2010 2:07 PM
To: 'program-java@xxxxxxxxxxxxx'
Subject: [program-java] Java: Precedence... solution



Hey now:



I've finally resolved the issue I was having with variables being reused 
multiple times in the same class...



For the sake of brevity:



Public class MyClass {



Private int x = 1;



Public MyClass() {

myMethod();

}



Private void myMethod() {

For (x =0; x<10; x++) {

}

}

}



...obviously x gets finally incremented to 9 and since it's global then it 
throws off any reuse of the variable.



James M. Corbett



Programmer / Analyst |

Canada Revenue Agency | Agence du revenue du Canada

875 Heron Rd.

Ottawa, On.

K1A0L5



James.Corbett@xxxxxxxxxxxxx

Telephone | Téléphone: (613) 941-1338

Facsimile | Télécopieur: (613) 941-2261



Government of Canada | Gouvernement du Canada






________________________________

This e-mail and any attachments to it are confidential and are intended solely 
for use of the individual or entity to whom they are addressed. If you have 
received this e-mail in error, please notify the sender immediately and then 
delete it. If you are not the intended recipient, you must not keep, use, 
disclose, copy or distribute this e-mail without the author's prior permission. 
The views expressed in this e-mail message do not necessarily represent the 
views of Highmark Inc., its subsidiaries, or affiliates.





Other related posts: