0% found this document useful (0 votes)
26 views8 pages

Class Structure

The document discusses the basic building blocks of Java classes including fields, methods, objects, and comments. Some key points: - Classes are the basic building blocks and contain fields to store data and methods to perform actions. - An object is an instance of a class that represents real world entities. Objects are created using the new operator. - Fields store the data/state of an object while methods perform actions on that data. - Comments are used to explain code and are ignored by the compiler. There are different styles like single-line, multi-line, and Javadoc comments. - Each class is typically in its own .java file and only one class can be public per file if it

Uploaded by

abilgen006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views8 pages

Class Structure

The document discusses the basic building blocks of Java classes including fields, methods, objects, and comments. Some key points: - Classes are the basic building blocks and contain fields to store data and methods to perform actions. - An object is an instance of a class that represents real world entities. Objects are created using the new operator. - Fields store the data/state of an object while methods perform actions on that data. - Comments are used to explain code and are ignored by the compiler. There are different styles like single-line, multi-line, and Javadoc comments. - Each class is typically in its own .java file and only one class can be public per file if it

Uploaded by

abilgen006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Building Blocks

Class Structure
Classes
• classes are basic building blocks of every Java program
• to design a class means to describe parts and characteristics of these blocks
• in order to use a class you need to create an object (most of the times)
• you can think about a class as a blueprint, and an object as realization
• an object is a single representation of the class, also called instance of a class
• a reference is a variable that points to an object
Fields and Methods
• two main elements (members) of Java classes are elds and methods
• elds are sometimes referred to variables
• to be precise: all elds are variables, but not all variables are elds
• elds hold the information about the state of an object or a class
• methods describe some action or operation on that state
• methods are similar to functions in some older programming languages
• let's write some code...
fi
fi
fi
fi
fi
// simplest Java class

class Student { }

// in file Student.java

public class Student { return type


String name;

public String getName() {

return name; signature


}

public void setName(String theName) {

name = theName;

}
Comments
• comments are used to make a code more readable
• they are ignored by the compiler
• there are three ways to comment out the text
• comment until the end of a line: //
• comment everything within /* and */
• comment starting with /** (Javadoc)
// this is one-line comment

System.out.println(a); // this will print a

System.out.println(a); /* this will print a */

/*

* usual way to write multiline comments

* it's very readable like this

*/

/**

* Javadoc style offers you some nice features

* @author Luka Popov

*/
Classes and source files
• it's a good practice to have each class in it's own .java le
• it's possible to have more classes in one le
• but only one of them is top-level class
• top-level class is almost always marked as public, but it's not necessary
• if you mark the top-level class with public, then the lename must match
the class name

• only one class in the can be marked as public


fi
fi
fi
// in file Student.java

public class Student { }

// in file Item.java

public class Item { }

class SomeOtherItem { }

// in file Customer.java

public class Customer { }

public class Client { } // DOES NOT COMPILE

You might also like