• Post Reply Bookmark Topic Watch Topic
  • New Topic

A question regarding making an instance of a class as private member  RSS feed

 
dan kom
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!,  when using an object from another class,  is it right to make an instance of it as a private member and giving it some default parameters? (row 5-6) because if i just type :  private Date _departureDate;  i would get null on run-time. thanks
(in the question all variables _x are given to us as certain type.
* class works fine using testers and outputs from our forum.

 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I know what you mean but some of your phrasing and terminology is not quite right.

On lines 5 & 6, you declare private instance variables of type Date and assign them default values. If you hadn't done that, Java would set them to null by default. There is nothing right or wrong about either approach per se, it just depends on the requirements of your program what is the most appropriate thing to do.

As for declaring instance variables as private, well, that's the recommended way to declare them if you want to maintain encapsulation. Only you class should have access to its internal fields.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You do realise that is not 1st January?
It would be better to initialise the fields via the constructor; that way they can have different values for each instance without your needing default values. You will however have to pass information to the constructor to initialise all the fields.
It is unfortunate that you have been told to use Date for a variable starting _ for two reasons:
  • 1: The Date class has largely been superseded by the classes described in this tutorial.
  • 2: Java® identifiers shou‍ld not start _
  •  
    Jesper de Jong
    Java Cowboy
    Sheriff
    Posts: 16057
    88
    Android IntelliJ IDE Java Scala Spring
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Why are you specifically asking about making member variables private? This has nothing to do with whether those member variables are private, protected or public or don't have an access modifier at all.

    You have to understand the difference between variables and objects. These are not the same thing. A variable is a reference to an object. If you only declare a variable, it is not pointing to any object. If you don't assign a value to a member variable (which means: make it refer to an object), then it will automatically be initialized to the special value null which means that the variable refers to no object at all.

    So, if you want a member variable such as _departureDate to point to a Date object, then you have to assign a value to it somewhere. Either on the same line as you are doing in your code, or in a constructor, or in a method.
     
    Jesper de Jong
    Java Cowboy
    Sheriff
    Posts: 16057
    88
    Android IntelliJ IDE Java Scala Spring
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Another point: You are using the constructor of class Date the wrong way.

    First of all, the constructor that takes three arguments that you are using, is deprecated, which means you should not use it.

    Second, you are passing it wrong parameters. Carefully look at the API documentation of this constructor. It takes three arguments: the year minus 1900, the zero-based month (between 0 and 11) and the day of the month.

    If you do new Date(1, 1, 2000) you get a Date object that refers to the 2,000th day of February of the year 1901. This means you'll end up with a Date object that refers to the date 24 July 1906. That is not what you meant!
     
    dan kom
    Ranch Hand
    Posts: 31
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:You do realise that is not 1st January?
    It would be better to initialise the fields via the constructor


    I've created a Date class before the Trip class i'm. We are told to have two constructors for the Date class: Public Date(int day, int month, int year) and copy constructor   Public Date(Date date) .

    How can i do that as you suggested?



     
    Jesper de Jong
    Java Cowboy
    Sheriff
    Posts: 16057
    88
    Android IntelliJ IDE Java Scala Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Aha, so you are using your own class named Date instead of the standard class named Date in the package java.util.

    That is confusing Campbell and me. In general it is not a good idea to give your own classes the same names as classes in the standard Java library, because that can easily cause confusion.
     
    dan kom
    Ranch Hand
    Posts: 31
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jesper de Jong wrote:Aha, so you are using your own class named Date instead of the standard class named Date in the package java.util.

    That is confusing Campbell and me. In general it is not a good idea to give your own classes the same names as classes in the standard Java library, because that can easily cause confusion.


    Agree but it's not like i have a choice , the uni gives the orders ;)
     
    Henry Wong
    author
    Sheriff
    Posts: 23295
    125
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    dan kom wrote:
    Agree but it's not like i have a choice , the uni gives the orders ;)


    The issue is confusion. You should have mentioned that right from the beginning. Otherwise, this confusion will lead to confusing answers.

    Henry
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!