
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Structure and Members of the Java Program
While writing any piece of code in Java, there is a certain set of rules and regulations that need to be followed, that is considered as a standard. For example − A class contains variables, and functions. The functions can be used to work with the variables. Classes can be extended, and improvised too.
Basic structure
List of packages that are imported; public class <class_name> { Constructor (can be user defined or implicitly created) { Operations that the constructor should perform; } Data elements/class data members; User-defined functions/methods; public static void main (String args[]) extends exception { Instance of class created; Other operations; } }
Execution of a Java program begins from the ‘main’ function. Since it doesn’t return anything, its return type is void. It should be accessible by the code hence it is ‘public’.
Constructors are used to initialize the objects of a class that is previously defined. They can’t be declared with the keywords ‘final’, ‘abstract’ or ‘static’ or ‘synchronized’.
On the other hand, user defined functions perform specific tasks and can be used with keywords ‘final’, ‘abstract’ or ‘static’ or ‘synchronized’.
Example
public class Employee { static int beginning = 2017; int num; public Employee(int i) { num = i; beginning++; } public void display_data() { System.out.println("The static value is : " + beginning + "\n The instance value is :"+ num); } public static int square_val() { return beginning * beginning; } public static void main(String args[]) { Employee emp_1 = new Employee(2018); System.out.println("First object created "); emp_1.display_data(); int sq_val = Employee.square_val(); System.out.println("The square of the number is : "+ sq_val); } }
Output
First object created The static value is : 2018 The instance value is :2018 The square of the number is : 4072324
A class named Employee has different attributes and a constructor is defined that increments one of the attributes of the class. A function named ‘display_data’ displays the data present in the class.Another function named ‘square_val’ returns the square of a specific number. In the main function, an instance of the class is created and the functions are called. Relevant output is displayed on the console.
- Related Articles
- Structure and Members of the C# Program
- Write a C program to display the size and offset of structure members
- Locate unused structures and structure-members
- Java Program Structure
- Flexible Array Members in a structure in C
- Explain the basic structure of a program in Java?
- Declaring a structure with no members in C language
- Java Program to Implement the graph data structure
- Java Program to Implement the queue data structure
- How to pass the individual members as arguments to function using structure elements?
- How to pass individual members of structure as arguments to function in C language?
- How do we compare the members of enums in Java?
- C++ program to find sequence of indices of the team members
- What are static members of a Java class?
- Comparing enum members in Java\n
