#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    3

    Java's default access modifiers


    Hi.
    I was wondering what is the default access modifier in Java when I don't specify it.
    So I googled it, and from what I was reading, I understand that when you don't specify access modifiers for a class, the instance fields will not be accessible from 'outside' (when I say 'outside', I mean classes that are not sub classes of the class in question, and have no relation to the class in question).

    But look at this code:

    Code:
    class Dog {
    
            int size;
            String name;
            String breed;
    
    
            void bark() {
                System.out.println("Waff! Waff!");
            }
    
    
            void setSize(int aSize) {
                size=aSize;
            }
    
            int getSize() {
                return size;
            }
    
    }

    Code:
    class DogTestDrive {
    
            public static void main(String []args) {
                    Dog aDog=new Dog();
                    aDog.name="Lucky";
                    aDog.size=12;
                    System.out.println(aDog.name+"'s size is: "+aDog.size+".");
            }
    }
    for some reason, I was able to compile and run this code, even though, as you can see, DogTestDrive.class has no relation with Dog.class .

    can someone please clarify this one for me?
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,730
    Rep Power
    1959
    You haven't specified any package name which mean both classes are in the same "standard/unnamed package".
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    3
    Hi MrFujin, thanks for the help.

    well, I don't know what packages are (I didn't get to that part in the book yet), but that's good enough.
    I just wanted to make sure I wasn't missing anything there.
    Last edited by so.very.tired; August 27th, 2013 at 12:56 PM.

IMN logo majestic logo threadwatch logo seochat tools logo