Download presentation
Presentation is loading. Please wait.
Published byBrenden Sarge Modified over 9 years ago
2
Classes All code in a Java program is part of a class A class has two purposes Provide functions to do work for the programmer Represent data Two kinds of things found in a class Variables store data about the class Methods provide programmers with ways of asking the class to do work
3
Files in a Java program A Java program is always made up of one or more classes Most files in a Java program have a single class A Java program will have many different files, typically one for each class. The code is stored in a file with the same name as the class and the extension.java. The java compiler creates a file with the same name, but the extension.class.
4
Defining a Class class myClass {... } 1. Starts with the word “class” 2. Followed by the class name 3. Followed by an open curly bracket 4. Followed by method and variable definitions. 5. Followed by a close curly bracket
5
Methods A method is a piece of java code that does a job Defined within the curly braces of a class definition Two types 1. Static methods (also called class methods) do a job for a programmer 2. Instance methods manipulate the data within a class For now we will talk about static methods
6
The parts of a method 1. Visibility Public - any part of the Java program can use it Private - can only be used within the same class 2. Type of method (static?) 3. Return type (what type of information it returns) 4. Name 5. Parameters - Data given to the method to do its job. 6. Body - The statements that get executed when the method is called.
7
Parts of a Method public static void main (String[ ] args) { System.out.print(“Hey there!”); } 1. Visibility 2. Static 3. Return type (void means nothing returned) 4. Method name 5. List of parameters 6. Body inside curly brackets
8
Indenting Classes Variables and methods in a class should be indented Close bracket at end should be indented the same as class Open bracket can be on same line as class or next line Classes are usually in their own file class myClass {//start of myClass void method1() {//start of method1 code; }//end method1 }// end myClass
9
Indenting Methods and Functions Statements inside the method or function must be indented Close bracket at end should be indented the same as the definition Open bracket can be on same line or next line void myMethod() { statement1; }
10
Running a Java method The body of a method between the curly brackets contain one or more statements Statements are separated from one another by semicolons When a method is called, it executes the statements one at a time When Java is run, it looks for a method called main and runs it The main method can call other methods, either in the same class or in other classes.
11
Some common methods System.out.println(“I pwn all noobs”); System.out.println prints whatever is inside the parentheses to the console Moves the console cursor to a new line when it is done. System.out.print(“U just got served”); Similar to System.out.println, but does not move the cursor to a new line.
12
Formatting Java Programs Java is case sensitive. E.g. System.out.println is not the same as system.out.println. Java is white space insensitive Can put any number of spaces or new line characters between statements Should use spaces to indent to make programs readable // (two slashes) in Java start a comment: // This is a comment Everything from the // to the end of line is ignored
13
Summary Java programs are made up of classes Classes have variables that store data Methods do work for the programmer Class and method definitions have multiple parts. The body of a method contains one or more statements. End with a semicolon Are executed in order when the method is run The main method is run when a Java program starts System.out.print/println send output to the console Comments and extra white space are ignored
14
Project 0 Work on Project 0 Due date : Tomorrow!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.