DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2005
    Location
    Melbourne...Australia
    Posts
    279

    How to access data members from another class..

    I am trying to access private data members from one class in another class..
    eg: Trying to use a print() method in one class and trying to access the data members with getID()..

    However this throws and error "Non static method cannot be referenced from static context.."

    What have I done wrong and how could I correct it? Code snippets below..

    Part of my print()
    Code:
    public void inorderPrint()
                    {
                        if (left != null)
                            left.inorderPrint( );
                        System.out.print(CD.getID()+"\t");
                        if (right != null)
                            right.inorderPrint( ); }
    Part of my CD class
    Code:
    public class CD {
                    private int ID;
                    private String artist;
                    private String title;
                    private String company;
        
        /** Creates a new instance of CD */
        public CD(int id, String ar, String ti, String co)
        { 
            ID = id;
            artist = ar;
            title = ti;
            company = co; 
        }
    public int getID()
        { return ID; }
        
        public String getArtist()
        { return artist; }
        
        public String getTitle()
        { return title; }
        
        public String getCompany()
        { return company; }
    }
    Thankyou for your time..

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    I see one suspicious line in the inorderPrint()
    Code:
    System.out.print(CD.getID()+"\t");
    you cannot use the CD class' getID method like it was a static method,
    and if you think of it, what id should that method return ?
    eschew obfuscation

  3. #3
    Join Date
    Aug 2005
    Location
    Melbourne...Australia
    Posts
    279
    Thankyou for your reply..
    That is what I am having problems with, how to access the members of the CD class in my BTNode class?

  4. #4
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    I'll have to be rather general here...

    If you have two classes ClassA and ClassB and you want ClassB to invoke
    non-static methods in ClassA then ClassB must have access to an instance of
    ClassA. You can do this many ways but two of them are:
    1: ClassB has a constructor that takes an instance of ClassA
    2: ClassB has a method, e.g. setClassA(ClassA aClass), that supplies it
    with an instance of ClassA.

    So, using method 1:
    Code:
    ClassA aClass=new ClassA();
    ClassB bClass=new ClassB(aClass);
    A part of ClassB:
    Code:
    private ClassA aClass=null;
    private int id=-1;
    
    // constructor
    public ClassB(ClassA aClass) {
      this.aClass=aClass;
    }
    public void someBMethod() {
      this.id=aClass.getID(); // invoke a ClassA method
    }
    Last edited by sjalle; 10-20-2005 at 11:23 AM. Reason: typo
    eschew obfuscation

  5. #5
    Join Date
    Aug 2005
    Location
    Melbourne...Australia
    Posts
    279
    Thankyou for your help..

    I now have the problem sorted!

    Cheers

Similar Threads

  1. Informix Data Access Components
    By alex in forum Database
    Replies: 0
    Last Post: 03-27-2003, 05:01 AM
  2. Replies: 60
    Last Post: 09-13-2002, 05:41 PM
  3. Set Scope of Class Members Dynamically
    By Ian Diaz in forum .NET
    Replies: 1
    Last Post: 08-31-2002, 11:50 AM
  4. PROBLEMS TO ACCESS INFORMIX“S DATA FROM VB
    By RADIAZMTZ in forum VB Classic
    Replies: 0
    Last Post: 04-24-2002, 03:16 PM
  5. Access & SQL Server
    By David Jones in forum Database
    Replies: 0
    Last Post: 08-31-2001, 12:22 PM

Bookmarks

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


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links