[Go to site: main page, start]

0% found this document useful (0 votes)
8 views16 pages

Java Tutorial References Oracle Docs Only

This document serves as a tutorial on Java classes and objects, explaining the object-oriented nature of Java and how to create classes and objects. It includes examples of creating a class named 'Main', instantiating objects, and using multiple classes for better organization. Additionally, it provides a list of official Oracle Java documentation links for further reading.

Uploaded by

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

Java Tutorial References Oracle Docs Only

This document serves as a tutorial on Java classes and objects, explaining the object-oriented nature of Java and how to create classes and objects. It includes examples of creating a class named 'Main', instantiating objects, and using multiple classes for better organization. Additionally, it provides a list of official Oracle Java documentation links for further reading.

Uploaded by

1112eee
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java Tutorial – External References (Oracle Java Docs

Only)

This reference list contains only official Oracle Java documentation links (Java SE / JDK).
Use it as a bibliography or “Further Reading” section for a Java tutorial.

Java Classes/Objects

Java is an object-oriented programming language.

Everything in Java is associated with classes and objects, along with its attributes and
methods. For example: in real life, a car is an object. The car has attributes, such as weight
and color, and methods, such as drive and brake.

A Class is like an object constructor, or a "blueprint" for creating objects.

Create a Class

To create a class, use the keyword class.

In this example, we create a class named "Main" with a variable x:

[Link]

public class Main {

int x = 5;

Remember from the Java Syntax chapter that a class should always start with an uppercase
first letter, and that the name of the java file should match the class name.

Create an Object

In Java, an object is created from a class. We have already created the class named Main, so
now we can use this to create objects.

To create an object of Main, specify the class name, followed by the object name, and use the
keyword new:

ExampleGet your own Java Server


Create an object called "myObj" and print the value of x:

public class Main {

int x = 5;

public static void main(String[] args) {

Main myObj = new Main();

[Link](myObj.x);

Multiple Objects

You can create multiple objects of one class:

Example

Create two objects of Main:

public class Main {

int x = 5;

public static void main(String[] args) {

Main myObj1 = new Main(); // Object 1

Main myObj2 = new Main(); // Object 2

[Link](myObj1.x);

[Link](myObj2.x);

}
Using Multiple Classes

You can also create an object of a class and access it in another class. This is often used for
better organization of classes (one class has all the attributes and methods, while the other
class holds the main() method (code to be executed)).

Remember that the name of the java file should match the class name. In this example, we
have created two files in the same directory/folder:

 [Link]

 [Link]

[Link]

public class Main {

int x = 5;

[Link]

class Second {

public static void main(String[] args) {

Main myObj = new Main();

[Link](myObj.x);

When both files have been compiled:

C:\Users\Your Name>javac [Link]


C:\Users\Your Name>javac [Link]

Run the [Link] file:

C:\Users\Your Name>java Second

And the output will be:


5

Java Classes/Objects

Java is an object-oriented programming language.

Everything in Java is associated with classes and objects, along with its attributes and
methods. For example: in real life, a car is an object. The car has attributes, such as weight
and color, and methods, such as drive and brake.

A Class is like an object constructor, or a "blueprint" for creating objects.

Create a Class

To create a class, use the keyword class.

In this example, we create a class named "Main" with a variable x:

[Link]

public class Main {

int x = 5;

Remember from the Java Syntax chapter that a class should always start with an uppercase
first letter, and that the name of the java file should match the class name.

Create an Object

In Java, an object is created from a class. We have already created the class named Main, so
now we can use this to create objects.

To create an object of Main, specify the class name, followed by the object name, and use the
keyword new:

ExampleGet your own Java Server

Create an object called "myObj" and print the value of x:

public class Main {

int x = 5;
public static void main(String[] args) {

Main myObj = new Main();

[Link](myObj.x);

Multiple Objects

You can create multiple objects of one class:

Example

Create two objects of Main:

public class Main {

int x = 5;

public static void main(String[] args) {

Main myObj1 = new Main(); // Object 1

Main myObj2 = new Main(); // Object 2

[Link](myObj1.x);

[Link](myObj2.x);

Using Multiple Classes


You can also create an object of a class and access it in another class. This is often used for
better organization of classes (one class has all the attributes and methods, while the other
class holds the main() method (code to be executed)).

Remember that the name of the java file should match the class name. In this example, we
have created two files in the same directory/folder:

 [Link]

 [Link]

[Link]

public class Main {

int x = 5;

[Link]

class Second {

public static void main(String[] args) {

Main myObj = new Main();

[Link](myObj.x);

When both files have been compiled:

C:\Users\Your Name>javac [Link]


C:\Users\Your Name>javac [Link]

Run the [Link] file:

C:\Users\Your Name>java Second

And the output will be:

Java Classes/Objects

Java is an object-oriented programming language.


Everything in Java is associated with classes and objects, along with its attributes and
methods. For example: in real life, a car is an object. The car has attributes, such as weight
and color, and methods, such as drive and brake.

A Class is like an object constructor, or a "blueprint" for creating objects.

Create a Class

To create a class, use the keyword class.

In this example, we create a class named "Main" with a variable x:

[Link]

public class Main {

int x = 5;

Remember from the Java Syntax chapter that a class should always start with an uppercase
first letter, and that the name of the java file should match the class name.

Create an Object

In Java, an object is created from a class. We have already created the class named Main, so
now we can use this to create objects.

To create an object of Main, specify the class name, followed by the object name, and use the
keyword new:

ExampleGet your own Java Server

Create an object called "myObj" and print the value of x:

public class Main {

int x = 5;

public static void main(String[] args) {

Main myObj = new Main();

[Link](myObj.x);
}

Multiple Objects

You can create multiple objects of one class:

Example

Create two objects of Main:

public class Main {

int x = 5;

public static void main(String[] args) {

Main myObj1 = new Main(); // Object 1

Main myObj2 = new Main(); // Object 2

[Link](myObj1.x);

[Link](myObj2.x);

Using Multiple Classes

You can also create an object of a class and access it in another class. This is often used for
better organization of classes (one class has all the attributes and methods, while the other
class holds the main() method (code to be executed)).
Remember that the name of the java file should match the class name. In this example, we
have created two files in the same directory/folder:

 [Link]

 [Link]

[Link]

public class Main {

int x = 5;

[Link]

class Second {

public static void main(String[] args) {

Main myObj = new Main();

[Link](myObj.x);

When both files have been compiled:

C:\Users\Your Name>javac [Link]


C:\Users\Your Name>javac [Link]

Run the [Link] file:

C:\Users\Your Name>java Second

And the output will be:

Java Classes/Objects

Java is an object-oriented programming language.

Everything in Java is associated with classes and objects, along with its attributes and
methods. For example: in real life, a car is an object. The car has attributes, such as weight
and color, and methods, such as drive and brake.

A Class is like an object constructor, or a "blueprint" for creating objects.


Create a Class

To create a class, use the keyword class.

In this example, we create a class named "Main" with a variable x:

[Link]

public class Main {

int x = 5;

Remember from the Java Syntax chapter that a class should always start with an uppercase
first letter, and that the name of the java file should match the class name.

Create an Object

In Java, an object is created from a class. We have already created the class named Main, so
now we can use this to create objects.

To create an object of Main, specify the class name, followed by the object name, and use the
keyword new:

ExampleGet your own Java Server

Create an object called "myObj" and print the value of x:

public class Main {

int x = 5;

public static void main(String[] args) {

Main myObj = new Main();

[Link](myObj.x);

}
Multiple Objects

You can create multiple objects of one class:

Example

Create two objects of Main:

public class Main {

int x = 5;

public static void main(String[] args) {

Main myObj1 = new Main(); // Object 1

Main myObj2 = new Main(); // Object 2

[Link](myObj1.x);

[Link](myObj2.x);

Using Multiple Classes

You can also create an object of a class and access it in another class. This is often used for
better organization of classes (one class has all the attributes and methods, while the other
class holds the main() method (code to be executed)).

Remember that the name of the java file should match the class name. In this example, we
have created two files in the same directory/folder:

 [Link]

 [Link]
[Link]

public class Main {

int x = 5;

[Link]

class Second {

public static void main(String[] args) {

Main myObj = new Main();

[Link](myObj.x);

When both files have been compiled:

C:\Users\Your Name>javac [Link]


C:\Users\Your Name>javac [Link]

Run the [Link] file:

C:\Users\Your Name>java Second

And the output will be:

Java Classes/Objects

Java is an object-oriented programming language.

Everything in Java is associated with classes and objects, along with its attributes and
methods. For example: in real life, a car is an object. The car has attributes, such as weight
and color, and methods, such as drive and brake.

A Class is like an object constructor, or a "blueprint" for creating objects.

Create a Class

To create a class, use the keyword class.


In this example, we create a class named "Main" with a variable x:

[Link]

public class Main {

int x = 5;

Remember from the Java Syntax chapter that a class should always start with an uppercase
first letter, and that the name of the java file should match the class name.

Create an Object

In Java, an object is created from a class. We have already created the class named Main, so
now we can use this to create objects.

To create an object of Main, specify the class name, followed by the object name, and use the
keyword new:

ExampleGet your own Java Server

Create an object called "myObj" and print the value of x:

public class Main {

int x = 5;

public static void main(String[] args) {

Main myObj = new Main();

[Link](myObj.x);

Multiple Objects
You can create multiple objects of one class:

Example

Create two objects of Main:

public class Main {

int x = 5;

public static void main(String[] args) {

Main myObj1 = new Main(); // Object 1

Main myObj2 = new Main(); // Object 2

[Link](myObj1.x);

[Link](myObj2.x);

Using Multiple Classes

You can also create an object of a class and access it in another class. This is often used for
better organization of classes (one class has all the attributes and methods, while the other
class holds the main() method (code to be executed)).

Remember that the name of the java file should match the class name. In this example, we
have created two files in the same directory/folder:

 [Link]

 [Link]

[Link]

public class Main {

int x = 5;
}

[Link]

class Second {

public static void main(String[] args) {

Main myObj = new Main();

[Link](myObj.x);

When both files have been compiled:

C:\Users\Your Name>javac [Link]


C:\Users\Your Name>javac [Link]

Run the [Link] file:

C:\Users\Your Name>java Second

And the output will be:

References

[1] Java SE Documentation (overview). [Link]


[Link]

[2] Java Platform, Standard Edition Documentation (release index).


[Link]

[3] JDK 17 Documentation Home. [Link]

[4] Java SE 17 API Specification (Overview).


[Link]

[5] [Link] module summary (Java SE 17).


[Link]
[6] API Help (Java SE 17 & JDK 17).
[Link]

[7] Java SE Specifications index. [Link]

[8] The Java Language Specification (JLS) — Java SE 17 PDF.


[Link]

[9] The Java Language Specification (JLS) — Java SE 17 HTML.


[Link]

[10] The Java Virtual Machine Specification (JVMS) — Java SE 17 PDF.


[Link]

[11] The Java Virtual Machine Specification (JVMS) — Java SE 17 HTML.


[Link]

[12] Java Tutorials (Oracle). [Link]

[13] Trail: Learning the Java Language (Oracle Tutorials).


[Link]

[14] Trail: Collections (Oracle Tutorials).


[Link]

[15] JDK 17 Release Notes landing page.


[Link]

Common questions

Powered by AI

Using multiple classes enhances code organization by separating the main executable code from class definitions. Each file should have a name matching the class it contains, such as 'Main.java' and 'Second.java', and both files should be in the same directory. Compile them separately with 'javac' command, for example, 'javac Main.java' and 'javac Second.java', then run the main file to execute .

In Java, attributes and methods define the properties and capabilities of objects. Using the analogy of a car, the car itself is an object, while attributes include its weight and color. Methods describe its behaviors, such as 'drive' or 'brake'. This concept highlights how objects encapsulate data and functionality, mirroring real-world entities and their characteristics .

Multiple objects of a single class are created using the same class blueprint, with each object being instantiated independently. For instance, 'Main myObj1 = new Main();' and 'Main myObj2 = new Main();' create two separate objects of the 'Main' class. This illustrates object-oriented programming as it allows for the creation of multiple instances of the same class, each maintaining its own state .

When a Java program is run with a class containing an integer variable initialized within the class definition, such as 'public class Main { int x = 5; }', and the value is printed, it will output '5'. This occurs because the variable 'x' is assigned a default value that is not altered before being printed .

Java's object-oriented principles enable interactions between classes through encapsulation and reuse. One class can instantiate and access objects from another class, as shown by using one class to hold attributes and another to execute methods. This design strategy supports robustness, modularity, and easier code maintenance, allowing developers to build scalable and flexible software architectures .

To create a class in Java, use the keyword 'class' followed by the class name, which should start with an uppercase letter. For example: 'public class Main { int x = 5; }'. To create an object of the class, use the 'new' keyword with the class name: 'Main myObj = new Main();'. The Java file name should match the class name, so in this case, the file should be 'Main.java' .

The 'main' method is the entry point for Java applications, typically found in the class containing the executable code. It is conventionally placed in the same directory as other related classes. This organization ensures a clear start point for program execution, simplifying the structure and execution flow of the application, which is essential for a manageable development process .

Class names in Java should start with an uppercase first letter, and the Java file name must match the class name. These conventions are crucial for code readability, maintainability, and avoiding conflicts during compilation as Java relies on these naming conventions to correctly map the physical file structure to the code structure .

Using separate classes for attributes and execution code promotes modularity, making larger applications more manageable and enhancing code reusability. This approach allows for clearer separation of concerns, minimizes dependencies, and simplifies testing and debugging processes, facilitating collaboration in team environments .

To compile a Java program with multiple classes, ensure that all Java files are in the same directory. Use 'javac' to compile each file, for instance: 'javac Main.java' and 'javac Second.java'. Run the class with the 'main' method using the 'java' command: 'java Second'. This executes the program starting from the specified class file .

You might also like