Accessing method variables


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Accessing method variables

Hybrid View

  1. #1
    Join Date
    Oct 2005
    Posts
    36

    Accessing method variables

    I have this problem accessing variables of a class from another class. I'll make a simple example to try and make myself clear.....

    Code:
    public class temp
      {
         public void temp()
           {
              String [] abc = new String [200];
              abc[1] = "johnsmith";
              abc[2] = "smithjohn";
           }
      }
    
    public class accessTemp
      {
         public void accessTemp()
           {
              temp x = new temp();
              System.out.println(x.abc[1]);
           }
       }
    An error message tells me: "cannot find symbol variable abc". Why does class accessTemp cannot access variable 'abc[1]' through instance of class temp 'x'?

    Thanks!

  2. #2
    Join Date
    Mar 2004
    Posts
    635
    because abs only exists with temp() method, because that is where you defined it. Declare the variable in the class but outside any methods or constructors. Also, temp() is not a constructor because you defined a return type. Constructors have no return types

    Code:
    public class Temp
    {
        public String[] abc = new String[200];
        
        public Temp()
        {
             abc[0] = "johnsmith";
             abc[1] = "smithjohn";
        }
    }

Similar Threads

  1. Accessing variables between classes?
    By balltheheed in forum Java
    Replies: 2
    Last Post: 04-03-2005, 09:09 AM
  2. Replies: 0
    Last Post: 05-27-2003, 11:12 AM
  3. Replies: 3
    Last Post: 04-03-2003, 12:14 PM
  4. Accessing DTS Global Variables???
    By TechDork in forum Database
    Replies: 0
    Last Post: 04-02-2003, 03:05 PM
  5. Replies: 0
    Last Post: 04-26-2001, 10:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center