You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by temp temp <mi...@yahoo.com> on 2006/11/14 21:13:42 UTC

Simple java question

    I declared  a private global field called aField.
      The same  variable I decalred in a method .
                      For example
  public class Test2 {
      private String aField = " aField ";
  
      public static void main(String[] args) {
          Test2 test2 = new Test2();
          test2.testField();
          System.out.println(test2.aField);
      }
  
      private void testField() {
          String aField = "test1";
          System.out.println(aField);
      }
  }
  In the  method testField if I want to access the global variable  aField is it possible ?
          Thanks  & Regards
      Miro
    
 
---------------------------------
Access over 1 million songs - Yahoo! Music Unlimited.

RE: Simple java question

Posted by "Asthana, Rahul" <Ra...@CIBC.com>.
You can do this-
      private void testField() {
          String aField = "test1";
          System.out.println(aField);
		 System.out.println("global="+this.aField);
      }
Also, name you class Struts2 instead of Test2 so that it does not feel out of place in this List.:-)

Cheers
Rahul

-----Original Message-----
From: temp temp [mailto:miroconnect@yahoo.com]
Sent: Tuesday, November 14, 2006 3:14 PM
To: user@struts.apache.org
Subject: Simple java question


    I declared  a private global field called aField.
      The same  variable I decalred in a method .
                      For example
  public class Test2 {
      private String aField = " aField ";
  
      public static void main(String[] args) {
          Test2 test2 = new Test2();
          test2.testField();
          System.out.println(test2.aField);
      }
  
      private void testField() {
          String aField = "test1";
          System.out.println(aField);
      }
  }
  In the  method testField if I want to access the global variable  aField is it possible ?
          Thanks  & Regards
      Miro
    
 
---------------------------------
Access over 1 million songs - Yahoo! Music Unlimited.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Simple java question

Posted by Tamas Szabo <sz...@gmail.com>.
Yep, and why we are at it make sure you don't just rename one of them to

afield or a_Field or something similar, you get the pattern :-D

It might seem to be a good idea and saves you from coming up with some
other name but it could cause headache to others or even yourself
later on ...

Tamas


On 11/15/06, Christopher Goldman <cg...@dhapdigital.com> wrote:
> On Wed, 2006-11-15 at 06:33 +0800, Tamas Szabo wrote:
> > Well, it isn't a global field is an instance variable of your class.
> > And there is another way to access it. Just rename either the instance
> > variable or the local variable.
> >
> > Tamas
>
> Right.  While it is possible to do this, it does make it difficult to
> read the code.  Better to use two different variable names.  Then you
> never have to worry about which variable you're actually using, or
> mistakenly use the instance variable because you *thought* the local one
> was still in scope, when it was not.
>
> Actually the one exception I employ is with setter methods:
>
> public void setFoo( Object foo ) {
>   this.foo = foo;
> }
>
> Chris
>
> --
> Christopher D. Goldman
> cgoldman@dhapdigital.com
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Simple java question

Posted by Christopher Goldman <cg...@dhapdigital.com>.
On Wed, 2006-11-15 at 06:33 +0800, Tamas Szabo wrote:
> Well, it isn't a global field is an instance variable of your class.
> And there is another way to access it. Just rename either the instance
> variable or the local variable.
> 
> Tamas

Right.  While it is possible to do this, it does make it difficult to
read the code.  Better to use two different variable names.  Then you
never have to worry about which variable you're actually using, or
mistakenly use the instance variable because you *thought* the local one
was still in scope, when it was not.

Actually the one exception I employ is with setter methods:

public void setFoo( Object foo ) {
  this.foo = foo;
}

Chris

-- 
Christopher D. Goldman
cgoldman@dhapdigital.com



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Simple java question

Posted by Tamas Szabo <sz...@gmail.com>.
Well, it isn't a global field is an instance variable of your class.
And there is another way to access it. Just rename either the instance
variable or the local variable.

Tamas


On 11/15/06, temp temp <mi...@yahoo.com> wrote:
> If I donot use this.aField implies that complier will never point to  global field ? and only way I can access the global variable is using  this ?
>   Thanks & Regards
>   Miro
>
> paz.periasamy@axa.com.au wrote:  Hello temp temp,
>
> Use this.aField inside the method.
>
>
>   public class Test2 {
>       private String aField = " aField ";
>
>       public static void main(String[] args) {
>           Test2 test2 = new Test2();
>           test2.testField();
>           System.out.println(test2.aField);
>       }
>
>       private void testField() {
>           String aField = "test1";
>           System.out.println(aField);
>           System.out.println(this.aField);
>       }
>   }
>
>
> Thanks and regards,
> Pazhanikanthan. P (Paz)
>
> Consultant for AXA,
> Senior Software Engineer,
> HCL Australia Services Pty. Ltd.
> Off   : +61-3-9618-4085
> Mob : +61-0411-354-838
>
>
>
>
> temp temp
> 15/11/2006 07:13 AM
> Please respond to "Struts Users Mailing List"
>
>         To:     user@struts.apache.org
>         cc:
>         Subject:        Simple java question
>
>
>     I declared  a private global field called aField.
>       The same  variable I decalred in a method .
>                       For example
>   public class Test2 {
>       private String aField = " aField ";
>
>       public static void main(String[] args) {
>           Test2 test2 = new Test2();
>           test2.testField();
>           System.out.println(test2.aField);
>       }
>
>       private void testField() {
>           String aField = "test1";
>           System.out.println(aField);
>       }
>   }
>   In the  method testField if I want to access the global variable  aField
> is it possible ?
>           Thanks  & Regards
>       Miro
>
>
> ---------------------------------
> Access over 1 million songs - Yahoo! Music Unlimited.
>
> _____________________________________________________________________
> This e-mail has been scanned for viruses by MCI's Internet Managed
> Scanning Services - powered by MessageLabs. For further information
> visit http://www.mci.com
>
> *********************************************************************************
> Important Note
> This email (including any attachments) contains information which is
> confidential and may be subject to legal privilege.  If you are not
> the intended recipient you must not use, distribute or copy this
> email.  If you have received this email in error please notify the
> sender immediately and delete this email. Any views expressed in this
> email are not necessarily the views of AXA.   Thank you.
> **********************************************************************************
>
>
>
> ---------------------------------
> Want to start your own business? Learn how on Yahoo! Small Business.
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Simple java question

Posted by pa...@axa.com.au.
Yes. You need to use the "this" qualifier to access the global variable.

Thanks and regards,
Pazhanikanthan. P (Paz)

Consultant for AXA,
Senior Software Engineer,
HCL Australia Services Pty. Ltd.
Off   : +61-3-9618-4085
Mob : +61-0411-354-838




temp temp <mi...@yahoo.com>
15/11/2006 07:22 AM
Please respond to "Struts Users Mailing List"
 
        To:     Struts Users Mailing List <us...@struts.apache.org>
        cc: 
        Subject:        Re: Simple java question


If I donot use this.aField implies that complier will never point to 
global field ? and only way I can access the global variable is using this 
?
  Thanks & Regards
  Miro

paz.periasamy@axa.com.au wrote:  Hello temp temp,

Use this.aField inside the method.


  public class Test2 {
      private String aField = " aField ";
 
      public static void main(String[] args) {
          Test2 test2 = new Test2();
          test2.testField();
          System.out.println(test2.aField);
      }
 
      private void testField() {
          String aField = "test1";
          System.out.println(aField);
          System.out.println(this.aField);
      }
  }


Thanks and regards,
Pazhanikanthan. P (Paz)

Consultant for AXA,
Senior Software Engineer,
HCL Australia Services Pty. Ltd.
Off   : +61-3-9618-4085
Mob : +61-0411-354-838




temp temp 
15/11/2006 07:13 AM
Please respond to "Struts Users Mailing List"
 
        To:     user@struts.apache.org
        cc: 
        Subject:        Simple java question


    I declared  a private global field called aField.
      The same  variable I decalred in a method .
                      For example
  public class Test2 {
      private String aField = " aField ";
 
      public static void main(String[] args) {
          Test2 test2 = new Test2();
          test2.testField();
          System.out.println(test2.aField);
      }
 
      private void testField() {
          String aField = "test1";
          System.out.println(aField);
      }
  }
  In the  method testField if I want to access the global variable  aField 

is it possible ?
          Thanks  & Regards
      Miro
 
 
---------------------------------
Access over 1 million songs - Yahoo! Music Unlimited.

_____________________________________________________________________ 
This e-mail has been scanned for viruses by MCI's Internet Managed 
Scanning Services - powered by MessageLabs. For further information 
visit http://www.mci.com

*********************************************************************************
Important Note
This email (including any attachments) contains information which is 
confidential and may be subject to legal privilege.  If you are not 
the intended recipient you must not use, distribute or copy this 
email.  If you have received this email in error please notify the 
sender immediately and delete this email. Any views expressed in this 
email are not necessarily the views of AXA.   Thank you.
**********************************************************************************


 
---------------------------------
Want to start your own business? Learn how on Yahoo! Small Business.

_____________________________________________________________________ 
This e-mail has been scanned for viruses by MCI's Internet Managed 
Scanning Services - powered by MessageLabs. For further information 
visit http://www.mci.com

*********************************************************************************
Important Note
This email (including any attachments) contains information which is 
confidential and may be subject to legal privilege.  If you are not 
the intended recipient you must not use, distribute or copy this 
email.  If you have received this email in error please notify the 
sender immediately and delete this email. Any views expressed in this 
email are not necessarily the views of AXA.   Thank you.
**********************************************************************************

Re: Simple java question

Posted by temp temp <mi...@yahoo.com>.
If I donot use this.aField implies that complier will never point to  global field ? and only way I can access the global variable is using  this ?
  Thanks & Regards
  Miro

paz.periasamy@axa.com.au wrote:  Hello temp temp,

Use this.aField inside the method.


  public class Test2 {
      private String aField = " aField ";
 
      public static void main(String[] args) {
          Test2 test2 = new Test2();
          test2.testField();
          System.out.println(test2.aField);
      }
 
      private void testField() {
          String aField = "test1";
          System.out.println(aField);
          System.out.println(this.aField);
      }
  }


Thanks and regards,
Pazhanikanthan. P (Paz)

Consultant for AXA,
Senior Software Engineer,
HCL Australia Services Pty. Ltd.
Off   : +61-3-9618-4085
Mob : +61-0411-354-838




temp temp 
15/11/2006 07:13 AM
Please respond to "Struts Users Mailing List"
 
        To:     user@struts.apache.org
        cc: 
        Subject:        Simple java question


    I declared  a private global field called aField.
      The same  variable I decalred in a method .
                      For example
  public class Test2 {
      private String aField = " aField ";
 
      public static void main(String[] args) {
          Test2 test2 = new Test2();
          test2.testField();
          System.out.println(test2.aField);
      }
 
      private void testField() {
          String aField = "test1";
          System.out.println(aField);
      }
  }
  In the  method testField if I want to access the global variable  aField 
is it possible ?
          Thanks  & Regards
      Miro
 
 
---------------------------------
Access over 1 million songs - Yahoo! Music Unlimited.

_____________________________________________________________________ 
This e-mail has been scanned for viruses by MCI's Internet Managed 
Scanning Services - powered by MessageLabs. For further information 
visit http://www.mci.com

*********************************************************************************
Important Note
This email (including any attachments) contains information which is 
confidential and may be subject to legal privilege.  If you are not 
the intended recipient you must not use, distribute or copy this 
email.  If you have received this email in error please notify the 
sender immediately and delete this email. Any views expressed in this 
email are not necessarily the views of AXA.   Thank you.
**********************************************************************************


 
---------------------------------
Want to start your own business? Learn how on Yahoo! Small Business.

Re: Simple java question

Posted by pa...@axa.com.au.
Hello temp temp,

Use this.aField inside the method.


  public class Test2 {
      private String aField = " aField ";
 
      public static void main(String[] args) {
          Test2 test2 = new Test2();
          test2.testField();
          System.out.println(test2.aField);
      }
 
      private void testField() {
          String aField = "test1";
          System.out.println(aField);
          System.out.println(this.aField);
      }
  }


Thanks and regards,
Pazhanikanthan. P (Paz)

Consultant for AXA,
Senior Software Engineer,
HCL Australia Services Pty. Ltd.
Off   : +61-3-9618-4085
Mob : +61-0411-354-838




temp temp <mi...@yahoo.com>
15/11/2006 07:13 AM
Please respond to "Struts Users Mailing List"
 
        To:     user@struts.apache.org
        cc: 
        Subject:        Simple java question


    I declared  a private global field called aField.
      The same  variable I decalred in a method .
                      For example
  public class Test2 {
      private String aField = " aField ";
 
      public static void main(String[] args) {
          Test2 test2 = new Test2();
          test2.testField();
          System.out.println(test2.aField);
      }
 
      private void testField() {
          String aField = "test1";
          System.out.println(aField);
      }
  }
  In the  method testField if I want to access the global variable  aField 
is it possible ?
          Thanks  & Regards
      Miro
 
 
---------------------------------
Access over 1 million songs - Yahoo! Music Unlimited.

_____________________________________________________________________ 
This e-mail has been scanned for viruses by MCI's Internet Managed 
Scanning Services - powered by MessageLabs. For further information 
visit http://www.mci.com

*********************************************************************************
Important Note
This email (including any attachments) contains information which is 
confidential and may be subject to legal privilege.  If you are not 
the intended recipient you must not use, distribute or copy this 
email.  If you have received this email in error please notify the 
sender immediately and delete this email. Any views expressed in this 
email are not necessarily the views of AXA.   Thank you.
**********************************************************************************