A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.w3resource.com/java-tutorial/java-program-structure.php below:

Website Navigation


Java Program Structure - w3resource

Java Program StructureLast update on August 19 2022 21:50:42 (UTC/GMT +8 hours) Description

Let’s use the example of HelloWorld Java program to understand structure and features of the class. This program is written on few lines, and its only task is to print “Hello World from Java” on the screen. Refer the following picture.

1.“package sct”:

It is package declaration statement. The package statement defines a namespace in which classes are stored. The package is used to organize the classes based on functionality. If you omit the package statement, the class names are put into the default package, which has no name. Package statement cannot appear anywhere in the program. It must be the first line of your program or you can omit it.

2.“public class HelloWorld”:

This line has various aspects of java programming.

a. public: This is access modifier keyword which tells compiler access to class. Various values of access modifiers can be public, protected,private or default (no value).

b. class: This keyword used to declare a class. Name of class (HelloWorld) followed by this keyword.

3. Comments section:

We can write comments in java in two ways.

a. Line comments: It starts with two forward slashes (//) and continues to the end of the current line. Line comments do not require an ending symbol.

b. Block comments start with a forward slash and an asterisk (/*) and end with an asterisk and a forward slash (*/).Block comments can also extend across as many lines as needed.

4. “public static void main (String [ ] args)”:

Its method (Function) named main with string array as an argument.

a. public: Access Modifier

b. static: static is a reserved keyword which means that a method is accessible and usable even though no objects of the class exist.

c. void: This keyword declares nothing would be returned from the method. The method can return any primitive or object.

d. Method content inside curly braces. { } asdfla;sd

5. System.out.println("Hello World from Java") :

a. System: It is the name of Java utility class.

b. out:It is an object which belongs to System class.

c. println: It is utility method name which is used to send any String to the console.

d. “Hello World from Java”: It is String literal set as argument to println method.

More Information regarding Java Class:

package sct;
public class Car {
private String color;
private int maxSpeed; 
public String carInfo(){
return color + " Max Speed:-" + maxSpeed;
}
//This is constructor of Car Class
Car(String carColor, int speedLimit){
this.color = carColor;
this.maxSpeed =speedLimit;
}
}

package sct;
//This is car test class to instantiate and call Car objects.
public class CarTest {
public static void main(String[] args) {
Car maruti = new Car("Red", 160);
Car ferrari = new Car ("Yellow", 400);
System.out.println(maruti.carInfo());
System.out.println(ferrari.carInfo());
}
}

Output of above CarTest java class is as below. We can run CarTest java program because it has main method. Main method is starting point for any java program execution. Running a program means telling the Java VIrtual Machine (JVM) to "Load theclass, then start executing its main () method. Keep running 'til all thecode in main is finished."

Common Programming guidelines:

abstract boolean break byte case catch char class const continue default do double clse extends final finally float for goto if implements import instanceof int interface long native new package private protected public return short static strictfp super switch synchronized this throw throws transient try void volatile while assert enum        

We will understand more about constructors, access modifiers in coming tutorials. The source code of sample discussed attached here to run directly on your system.

Summary

In next session we will discuss Setup Classpath/ Environment variable and Compiling, running and debugging Java programs.

Previous: Introduction
Next: Java Primitive data type


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4