Can we access variables declared as private


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Can we access variables declared as private

  1. #1
    Join Date
    Jan 2005
    Posts
    45

    Can we access variables declared as private

    Hi

    Can we access private variables?

    import java.awt.*;

    public class Professors{
    private String name;
    private int age;

    public Professors(String n, int a){
    name = n;
    age = a;
    }

    public boolean olderThan(Professors a){
    return (age > a.age);
    }
    }

    How come in this code we can do that? I remember my instructor said that to access a private instance variable, we need to write a get method to do that. Is that correct ? Thanks for answers.

  2. #2
    Join Date
    Sep 2004
    Posts
    150
    Private variables are accessible to the entire class they are made in.

    They are for your class to use. When another class tries to access them it should not be able to. That class should have to access any data you are willing to give through public get and set methods. That is proper style anyway.

    That way you control what can be accessed in your class. You can also make things "read only" by only supplying a "get" method but not a "set" method, for example. If they had direct access to your variables you would not be able to control this.

  3. #3
    Join Date
    Feb 2004
    Posts
    808

    Re: Can we access variables declared as private

    Originally posted by scracker
    Hi

    Can we access private variables?
    only within the same set of curly brackets

    Code:
    import java.awt.*;
    
    public class Professors{
     private String name;
     private int age;
     
     public Professors(String n, int a){
       name = n;
       age = a;
     }
     
     public boolean olderThan(Professors a){
       return (age > a.age);
     }
    }
    How come in this code we can do that?
    access is based on classes, not objects. all object instances of Professors can access each others private variables. try this:

    Code:
    import java.awt.*;
    
    public class Professors{
     private String name;
     private int age;
    
     public Professors(String n, int a){
       name = n;
       age = a;
     }
    
     public boolean olderThan(Professors a){
       return (age > a.age);
     }
    
     public static void main(String[] argv){
       Professors p1,p2;
       p1 = new Professors("bob", 10);
       p2 = new Professors("nob", 20);
       System.out.print(p1.olderThan(p2));
       System.out.print(p1.age);
     }
    
    }
    class test{
    
      int i;
    
      public test(Professors p){
        i = p.age;
      }
    }
    I remember my instructor said that to access a private instance variable, we need to write a get method to do that. Is that correct ?
    yes, because it allows you to control the access to the data.. maybe the data is in some raw format and needs making nice before it is given out..
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

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