Once you click post assignment, learners in the selected class will immediately be able to see and complete this assignment
Back to Classroom
Declaring Classes
Java Syntax Course
Engineers From These Top Companies and Universities Trust EXLskills
1M+ Professionals | 100+ Institutions
This is the EXLskills free and open-source Java Syntax Micro Course. It guides learners via explanation, demonstration, and thorough practice, from no more than a basic understanding of Java, to a moderate level of understanding regarding Java syntax.
Is this course FREE?
Yes, this a 100% free course that you can contribute to on GitHub here!
Everything that a program accomplishes is outlined between the first brace and the final brace of the class. The line public static void main (String[] args) show where the program starts running. Please take a look at the code below that shows a class with a main method.
ClassExample.java
package exlcode;
public class ClassExample {
// This creates new String named exampleVariable
private static String exampleVariable;
// This is the constructor for the class
// that takes in one parameter
public ClassExample(String exampleVariable){
this.exampleVariable = exampleVariable;
}
public static void main(String[] args) {
// This will be explained in Unit 8
ClassExample classexample = new ClassExample("Hello World!");
// This prints the String stored in exampleVariable
System.out.println(exampleVariable);
}
}
The class ClassExample is an example of a class declaration. The area between the braces is called the class body and provides all the code information within a certain class.
Please remember that if you are planning to run the class, the main method must exist and be declared exactly the same as it is show above. The class is separated into three different sections:
Fields: contains variables that are declared or declared and initialized with a value
Constructors: initialize the objects of a particular class. We will dive deeper into this concept when we examine and discuss objects
Methods: sets of code that are referred to by name and called upon at any point in the program.