Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Head First Java NOTES:
- A try/catch is when you try to do something and if you it works,
- it prints out the try part but if it doesn't work, it prints out
- the catch part of the code.
- Java is an object-oriented (OO) langauge. It's not like the old
- days when you had steam-driven compilers and wrote on monolithic
- source file with a pile of procedures.
- A class is a blueprint for an object, and that nearly everything
- in Java is an object.
- When you design a class, think about the objects that will be created
- from that class type:
- - things the object knows
- - things the object does
- For example, a shopping cart (class name = ShoppingCart):
- What the object KNOWS is...
- cartContents
- What it DOES is...
- addToCart()
- removeFromCart()
- checkOut()
- Another example is an alarm (class name = Alarm):
- What the object KNOWS is...
- alarmTime
- alarmMode
- What the object DOES is...
- setAlarmTime()
- getAlarmTime()
- isAlarmSet()
- snooze()
- What the object knows about itself is it's instance variables (state)
- What the object can do are called methods (behvaior)
- For example, a Song class' instance variables or
- what it knows would be (class name = Song):
- title
- artist
- What it'd know how to do, it's methods, would be:
- setTitle()
- setArtist()
- play()
- The instance variables represent an object's state (the data),
- and can have unique values for each object of that type.
- Think of instance as another way of saying object.
- A tester class is where you put the main method, in that main()
- method you create and access objects of your new class type.
- Here's an example:
- STEPS:
- 1. Write your class
- public class chair
- {
- int size; <----- Instance variables
- String brand; <----- Instance variables
- String type; <----- Instance variables
- public void sit() <----- a method
- {
- }
- }
- 2. Write a tester
- public class chairTester
- {
- public static void main(String args[])
- {
- // Chair test code goes here
- }
- }
- 3. In your tester, make an object and access
- the object's variables and methods
- public class chairTester
- {
- public static void main(String args[])
- {
- chair c = new chair();
- c.size = 40;
- c.brand = "kohl's";
- c.type = "wood";
- }
- }
- The two uses of main:
- - to test your real class
- - to launch/start your Java application
- ---------------------------------------------------------------------------------------------------
- WHO AM I?
- I am compiled from a .java file = class
- My instance variable values can be different from my buddy's values = object
- I behave like a template = class
- I like to do stuff = object, method
- I can have many methods = class, object
- I represent 'state' = instance variable
- I have behaviors = object, class
- I am located in objects = method, instance variable
- I live on the heap = object
- I am used to create object instances = class
- My state can change = object, instance variable
- I declare methods = class
- I can change at runtime = object, instance variable
- ---------------------------------------------------------------------------------------------------
- There are two types of variables: primitive and reference.
- Primitive is with the eight primitive main types which is:
- boolean char byte short int long float double
- Reference is:
- Dog myDog = new Dog();
- Arrays are always objects, whether they're declared to hold
- primitives or object references.
- Remember: a class describes what an object knows and what an
- object does.
- Arguments or parameters are values passed into the method.
- You should remember it like this:
- A method uses parameters. A caller passes arguments.
- Arguments are the things you pass into the methods.
- Parameters are nothing more than a local variable. A variable
- with a type and a name, that can be used inside the body
- of the method. But here's the important part:
- If a method takes a parameter, you must pass it something. And
- that something must be a value of the appropriate type.
- If you declare a method to return a value, you must return
- a value of the declared type (Or a value that is compatible
- with the declared type)!
- EXAMPLE:
- ----------------------------------------
- int theSecret = life.giveSecret();
- int giveSecret()
- {
- return 42;
- }
- ----------------------------------------
- Methods can have multiple parameters. Most importantly, if
- a method has parameters, you must pass arguments of the
- right type and order.
- Calling a two-parameter method, and sending it two arguments.
- EXAMPLE:
- ----------------------------------------
- void go()
- {
- TestStuff t = new TestStuff();
- t.takeTwo(12, 34);
- }
- void takeTwo(int x, int y)
- {
- int z = x + y;
- System.out.println("Total is " + z)
- }
- ----------------------------------------
- You can pass variables into a method, as long as the
- variable type matches the parameter type.
- EXAMPLE:
- ----------------------------------------
- void go()
- {
- int foo = 7;
- int bar = 3;
- t.takeTwo(foo, bar);
- }
- void takeTwo(int x, int y)
- {
- int z = x + y;
- System.out.println("Total is " + z)
- }
- ----------------------------------------
- BULLET POINTS:
- ----------------------------------------------------------------
- Classes define what an object knows and what an object does.
- Things an object knows are it's instance variables (state).
- Things an object does are its methods (behavior).
- Methods can use instance variables so that objects of the same
- type can behave differently.
- A method can have parameters, which means you can pass one or
- more values in to the method.
- The number and type of values you pass in must match the order
- and type of the parameters declared by the method.
- Values passed in and out of methods can be implicitly promoted
- to a larger type of explicitly cast to a smaller type.
- The value you pass as an argument to a method can be a literal
- value (2, 'c', etc.) or a variable of the declared parameter
- type (for example, x where x is an int variable). (There are
- other things as well that you can pass as arguments).
- A method must declare a return type. A void return type means
- the method doesn't return anything.
- If a method declares a non-void return type, it must return a
- value compatible with the declared return type.
- ----------------------------------------------------------------
- Getters and Setters let you get and set things. Instance variables
- usually. A Getter's sole purpose in life is to send back, as a
- return value, the value of whatever it is that particular Getter
- is supposed to be Getting. And by now, it's probably no surprise
- that a Setter lives and breathes for the chance to take an
- argument value and use it to set the value of an instance variable.
- Mark instance variables private. Mark getters and setters public.
- Encapsulation in a sense puts a force-field around the instance variables
- so no one does anything to them. Most instance variables are coded
- with certain assumptions about the boundaries of the values. Like, think
- of all the things that would break if negative numbers were allowed.
- Number of bathrooms in an office. Velocity of an airplane. Birthdays.
- Barbell weight. Cell phone numbers. Microwave oven power.
- Encapsulation lets you set boundaries by forcing other code to go through
- setter methods. That way, the setter method can validate the parameter and
- decide if it's do-able. Maybe the method will reject it and do nothing, or
- maybe it'll throw an Exception, or maybe the method will round the parameter
- sent in to the nearest acceptable value. The point is, you can do whatever
- you want in the setter method, whereas you can't do anything if your
- instance variables are public.
- The point to setters and getters too, is that you can change your mind
- later, without breaking anybody else's code! Imagine if half the people
- in your company used your class with public instance variables, and one
- day you suddenly realized, "Oops-there's something I didn't plan for
- with that value, I'm going to have to switch to a setter method." You
- break everyone's code. The cool thing about encapsulation is that you
- get to change your mind. And nobody gets hurt. The performance gain from
- using variables directly is so minuscule and would rarely, if ever, be
- worth it.
- Instance variables always get a default value. If you don't explicitly
- assign a value to an instance variable, or you don't call a setter
- method, the instance variable still has a value.
- Local variables do NOT get a default value! The compiler complains if you
- try to use a local variable before the variable is initialized.
- Instance variables are declared inside a class but not within a method.
- Local variables are declared within a method.
- Local variables MUST be initialized before use!
- Method parameters are virtually the same as local variables --- they're declared inside the method (well, technically they're declared in the argument list of the method rather than within the body of the method, but they're still local variables as opposed to Instance variables). But method parameters will never be uninitialized, so you'll never get a compiler error telling you that a parameter variable might not have been initialized. But that's because the compiler will give you an error if you try to invoke a method without sending arguments that the method needs.So parameters are ALWAYS initialized, because the compiler guarantees that methods are always called with arguments that match the parameters declared for the method, and the arguments are assigned (automatically) to the parameters.
Advertisement
Add Comment
Please, Sign In to add comment