New User Special Price Expires in

Let's log you in.

Sign in with Facebook


Don't have a StudySoup account? Create one here!


Create a StudySoup account

Be part of our community, it's free to join!

Sign up with Facebook


Create your account
By creating an account you agree to StudySoup's terms and conditions and privacy policy

Already have a StudySoup account? Login here

CSC 2310 Java Chapter 8 - Classes

by: Taylor Kahl

CSC 2310 Java Chapter 8 - Classes CSC 2310

Marketplace > Georgia State University > ComputerScienence > CSC 2310 > CSC 2310 Java Chapter 8 Classes
Taylor Kahl
GPA 4.21
View Full Document for 0 Karma

View Full Document


Unlock These Notes for FREE

Enter your email below and we will instantly email you these Notes for Princliples of Computer Programming

(Limited time offer)

Unlock Notes

Already have a StudySoup account? Login here

Unlock FREE Class Notes

Enter your email below to receive Princliples of Computer Programming notes

Everyone needs better class notes. Enter your email and we will send you notes for this class for free.

Unlock FREE notes

About this Document

These notes cover the first 2 weeks of our class, including textbook and lecture material for all of chapter 8 on classes
Princliples of Computer Programming
Kebina Manandhar
Class Notes




Popular in Princliples of Computer Programming

Popular in ComputerScienence

This 6 page Class Notes was uploaded by Taylor Kahl on Friday January 22, 2016. The Class Notes belongs to CSC 2310 at Georgia State University taught by Kebina Manandhar in Winter 2016. Since its upload, it has received 192 views. For similar materials see Princliples of Computer Programming in ComputerScienence at Georgia State University.

Similar to CSC 2310 at GSU

Popular in ComputerScienence


Reviews for CSC 2310 Java Chapter 8 - Classes


Report this Material


What is Karma?


Karma is the currency of StudySoup.

You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!

Date Created: 01/22/16
Building Java Programs Ch. 8: Classes Previously, we’ve learned to program sets of actions Classes allow object-oriented programming – programming sets of objects Class – represents a program or a template for new types of objects Object – programming entity that combines state and behavior  Every object is an individual instance of a class with its own state and behavior  State – set of values stored in an object  Behavior – set of actions an object can perform  Example: Class of objects: radios o States: on or off, what station it’s on, what volume it’s set to o Behaviors: change station, change volume, turn on/off, check what station is on *** Objects are not complete programs; must be used w/ client code Client – code that interacts w/ a class or its objects (this is where your main method is)  Client code sends message to object to perform a behavior  Benefit: objects are reusable by multiple clients  Example: programs we’ve written before have been clients for objects like String, Random, Scanner, etc Creating new objects and classes: To create a new object, must first create a class and specify:  How to construct objects of that type  The state stored in each object  The behavior each object can perform Extended example: creating a Point class – Point stores (x, y) coordinates  Object state, aka Fields – variables inside object that make up its internal state o Syntax: same for normal variables – [type] [name]; o Declared inside { } of class o Declaring a field inside a class assigns that field to EVERY object w/in the class  Creating a new class: public class Point { int x; int y; } o Each Point object will contain 2 fields: int x and int y ***Client code that uses Point will be a separate class, stored in a separate .java file  Creating a new object of type Point with name origin (typed into client code): Point origin = new Point(); o Values are automatically initialized to (0, 0) public class PointMain { //this is the client code, which will access class Point public static void main(String[] args) { Point p1 = new Point(); p1.x = 7; //access fields w/ objectName.field p1.y = 2; //point p1 is now (7,2) Writing methods within classes:  Object behavior, aka Instance Methods – method inside an object that operates on that object o The object is called an implicit parameter  Declaring a method called translate within class Point: public class Point { int x; int y; public void translate(int dx, int dy) { //will shift point’s location x += dx; y += dy; } } ***Method initialization does NOT contain “static” o Each object will have its own copy of the translate method  Calling the method in the client code: p1.translate(11, 6); //increase p1.x by 11; increase p1.y by 6  toString method: o trying to print point p1 gives a strange result o toString returns a string representation of an object o Every class has a default toString method, without you having to write it  Writing one will replace the default o Example toString method(makes a string representation of a point): public String toString() { return “(“ + x + “, “ + y + “)”; //prints x&y coordinates } o Calling in client code: System.out.print(p1.toString()); Object initialization using constructors:  A constructor initializes new objects as they are created o Rather than using multiple statements as before o w/o constructor: Point p1 = new Point(); p1.x = 7; p1.y = 2; o w/ constructor: Point p1 = new Point(7, 2); o but first, we have to write the constructor in the class o constructor will execute when client code uses new keyword  Writing a constructor: o Specify what parameters need to be passed and what to do with them o Begins with public followed by class’s name and any parameters o No return type o Example: constructor for a new point with (x, y) location: public Point(int initialX, int initialY) { x = initialX; y = initialY; }  Constructors act on the object that’s being created w/ new keyword o can refer to the object’s fields and methods directly  With a constructor created, it’s now illegal to create an object w/o parameters o point p1 = new Point(); Shadowing – when 2 variables have same name and same scope  normally illegal  except when 1 variable is a field in the class  Example: int x and int y are already defined as fields for Point objects o But it’s legal to use int x and int y to define new variables, such as: public Point(int x, int y)  Now typing x or y will refer to the local variable, not the field  Using this keyword allows you to refer to the implicit parameter (object’s) methods and fields – useful if 2 variables have same name  Example: public Point(int x, int y) { this.x = x; this.y = y; } where int x and int y refer to the local variables just created and this.x and this.y refer to the object’s fields  Other uses of “this” o this.method(parameters) – calls a method o this(parameters) – allows one constructor to call another o Example: public Point() { this (0, 0); //calls Point(int, int) constructor } Multiple constructors – A class can have multiple constructors for multiple ways to create new objects  Each constructor must have a different number or type of parameters  Example: a constructor w/ no parameters to make a point object w/ location (0,0) public Point() { x = 0; y = 0; }  Now there are 2 ways to construct points o Point p1 = new Point(5, -2); //(5, -2) o Point p2 = new Point(); //(0, 0) Encapsulation:  Hiding an object’s details from the client code  Forces abstraction, which separates external behavior from internal state and protects data  This way, client cannot directly access/modify objects  private keyword encapsulates object’s fields o private int x; o private int y = 2; o private fields are only visible inside Point class o To allow client code access, write methods inside Point class public int getX() { return x; } o Now for client to access x field: p1.getX(); Static keyword:  part of a class, not part of an object  static methods and fields are shared by all objects in a class, rather than each object having its own copy o private static int i; o public static void method() {


Buy Material

Are you sure you want to buy this material for

0 Karma

Buy Material

BOOM! Enjoy Your Free Notes!

We've added these Notes to your profile, click here to view them now.


You're already Subscribed!

Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'

Why people love StudySoup

Steve Martinelli UC Los Angeles

"There's no way I would have passed my Organic Chemistry class this semester without the notes and study guides I got from StudySoup."

Janice Dongeun University of Washington

"I used the money I made selling my notes & study guides to pay for spring break in Olympia, Washington...which was Sweet!"

Jim McGreen Ohio University

"Knowing I can count on the Elite Notetaker in my class allows me to focus on what the professor is saying instead of just scribbling notes the whole time and falling behind."


"Their 'Elite Notetakers' are making over $1,200/month in sales by creating high quality content that helps their classmates in a time of need."

Become an Elite Notetaker and start selling your notes online!

Refund Policy


All subscriptions to StudySoup are paid in full at the time of subscribing. To change your credit card information or to cancel your subscription, go to "Edit Settings". All credit card information will be available there. If you should decide to cancel your subscription, it will continue to be valid until the next payment period, as all payments for the current period were made in advance. For special circumstances, please email


StudySoup has more than 1 million course-specific study resources to help students study smarter. If you’re having trouble finding what you’re looking for, our customer support team can help you find what you need! Feel free to contact them here:

Recurring Subscriptions: If you have canceled your recurring subscription on the day of renewal and have not downloaded any documents, you may request a refund by submitting an email to

Satisfaction Guarantee: If you’re not satisfied with your subscription, you can contact us for further help. Contact must be made within 3 business days of your subscription purchase and your refund request will be subject for review.

Please Note: Refunds can never be provided more than 30 days after the initial purchase date regardless of your activity on the site.