Results 1 to 3 of 3
  1. #1
    rrp
    rrp is offline Member
    Join Date
    Dec 2007
    Posts
    3
    Rep Power
    0

    Default Member Variables

    I am sure this is a simple question.

    I have 2 simple classes

    One called package1.Date
    and one called package2.DateFormater

    In Date I have 3 member variables. day,month and year - all int.

    If I include and instantiate Date in the DateFormatter class and try and set the member variables it doesn't like it.

    However if I put DateFormater in the same package it is fine.

    Help???

    Robert

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    If one object is defined in one package and another object in another package the members of those objects (to be accessible) have to be "public". If both classes are stored in the same package they can have their default access rights, i.e. package scope (no keyword for this necessary).

    Java Code:
    package p;
    
    public class C {
       int x; // not reachable outside package p;
       public int y; // reachacble from everywhere
    }
    ... alternatively you can define accessors and mutators (getters and setters) for those member variables. You can leave the member variables private and make those methods public.

    kind regards,

    Jos

    ps. This should be all described in great detail in your textbook.

  3. #3
    rrp
    rrp is offline Member
    Join Date
    Dec 2007
    Posts
    3
    Rep Power
    0

Similar Threads

  1. What are Instance variables and static variables?
    By sandeshforu in forum New To Java
    Replies: 3
    Last Post: 09-09-2009, 05:48 PM
  2. Greetings From New Member...
    By palms_09@yahoo.com in forum New To Java
    Replies: 0
    Last Post: 01-09-2009, 01:50 AM
  3. Hello.Am a new Member
    By winkodes in forum Java Applets
    Replies: 0
    Last Post: 12-07-2008, 07:19 AM
  4. new member-need help
    By vandana in forum New To Java
    Replies: 2
    Last Post: 01-21-2008, 06:01 AM
  5. new member
    By riadmazloum in forum Introductions
    Replies: 1
    Last Post: 08-09-2007, 12:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •