Saturday, February 14, 2015

Head First Java: Chapter 4



  • · Variables come in two flavors: primitive and  reference.
  • · Variables must always be declared with a name  and a type.
  • · A primitive variable value is the bits representing the value (5, ‘a’, true, 3.1416, etc.).
  • · A reference variable value is the bits representing a way to get to an object on the heap.
  •   A reference variable is like a remote control.
  • · Using the dot operator (.) on a reference variable is like pressing a button on the remote control to access a method or instance variable.
  • A reference variable has a value of null when it is not referencing any object.
  • An array is always an object, even if the array  is declared to hold primitives. There is no such thing as a primitive array, only an array that   holds primitives.






·          Methods
o   void speak(){}
o   Call method with (.)
o   Methods have ( ) when called
o   Ex. person1.speak( );
 

·          Getters and Return Values
o   Getters
§  Ex. public int getAge( ){
Return age;
§   Must return correct type

·         Method Parameters
o   Inside ( ) are parameters
o   Call a method
o   (int value) or ( String name)
o   Person1.name = “Jael”
                       
                       

·         Setters and "this"
o   Ex. void setAge (int age){
this.age; }


This is the people class that shows the instance variable name being declared private and I use my getters and setters in my greeting method.

·       Encapsulation: Public, Private, Protected
o   Public = can access anywhere
o   Private = access in  same class only
o   Protected = can be accessed in sub-classes and packages
o   No modifier = same package only 
This is the people class that shows the instance variable name being declared private and I use my getters and setters in my greeting method.

This is the programs main method and here I  instantiate a people array and use my setter and getters to produce a greeting statement with user input.

 




No comments:

Post a Comment