Hi I wonder if somebody can help me to understand a few things in the following example:

// Fig. 8.17: PackageDataTest.java
// Package-access members of a class are accessible by other classes 
// in the same package.

public class PackageDataTest 
   public static void main( String[] args )
      PackageData packageData = new PackageData();

      // output String representation of packageData 
      System.out.printf( "After instantiation:\n%s\n", packageData );

      // change package access data in packageData object
      packageData.number = 77;     
      packageData.string = "Goodbye";

      // output String representation of packageData
      System.out.printf( "\nAfter changing values:\n%s\n", packageData );
   } // end main
} // end class PackageDataTest

// class with package access instance variables
class PackageData 
   int number; // package-access instance variable
   String string; // package-access instance variable

   // constructor
   public PackageData() 
      number = 0; 
      string = "Hello";
   } // end PackageData constructor

   // return PackageData object String representation
   public String toString() 
      return String.format( "number: %d; string: %s", number, string );
   } // end method toString
} // end class PackageData

In this line System.out.printf( "After instantiation:\n%s\n", packageData ); packageData is the name of the class, I didn't know that it could be used as a parameter. I assume it's calling the toString method? But can you actually do that, use the class name to call a method like that?! never seen that before!

Recommended Answers

All 3 Replies

First off, you need to recall that Java is case-sensitive. which means that PackageData and packageData are two different things. The former is the class name, the latter is an instance of the class declared in the main() method of PackageDataTest.

Second, the toString() method is something of an exceptional case in Java, as it is used implicitly whenever an object is coerced to a String value. So what is happening here is that the object packageData (not the class named PackageData) is being passed to printf() as its String representation, so packageData.toString() is automagically called.

also, quite important: you should adopt good coding habbits as soon as possible:

make your instance variables private and use mutators (setters and getters) to get to them from outside the class. this 'll help with encapsulation.

oh yes sorry of course, that's the object and not the class. Even so , so far I have only come across variables to be passed to printf(). SO if you pass an object as a string representation what happens?I understand the toString() gets called automatically when something - in this case an object - is passed to printf() I just never came across that...so if the whole object is passed to it, even its variables (number and string) are passed to printf() and because of that toString() can safely do ( "number: %d; string: %s", number, string );

@stultuske, no worries, I tend to follow good coding habits, I didn't write the above example, I took it as it is from a book. The reason why it has been written like that is because it was only meant ot explain package access

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.