23
NovDiwali Deal : Flat 20% off + 2 free self-paced courses + Free Ebook + $200 Voucher - SCHEDULE CALL
Java is considered as an object-oriented, class-based, general-purpose and concurrent programming language which was created in 1995 by the Green Team comprising James Gosling, Mike Sheridan and Patrick Naughton at Sun Microsystems for various devices with a digital interface like set-top boxes, televisions, etc. It is similar in features to C++ but comes endowed with some simple and some advanced features. It can be run on all the platforms and is free to access. Over the years, it has found use in the following peripherals, namely banking, IT, retail, Big Data, Stock Market, Scientific research, Android, web development, framework, etc.
Java serves as an excellent computing platform which can be used to develop various applications. It follows the logic of “Write once, Run anywhere” kind of programming language, i.e., the code which is compiled with Java can be run on all the platforms which support it. Some of the notable features which make it highly useful are as follows:
There are many concepts which you will have to master as you start programming in Java-like classes, methods, exceptions, variables, constants, etc. Learning all of these in one-go may lead to information overload, so it is better to approach each one of these separately. Here are the steps which you will have to follow to call a method in Java.
1). The method is basically equal to a function in C, which is used for reusing code. It is comprised of a set of statements, and the method itself can be invoked through another statement. When it is called or invoked, all the statements which form a part of the method are executed. E.g. as the below figure shows “public static void methodExample ( ) { }”. Although there is no code in it, it does carry some keywords before the name like public, static, and void.
Read: Java Programmer Resume Template Sample - Guide for Fresher & Experienced
package com.test.testing;
public class Test {
public static void methodexample (){
}
}
2). The meaning of the word “public” before the method is that the method can be called from anywhere like classes, even different packages as long the class can be imported. The public can thus be replaced by either private or even protected.
If nothing is mentioned or specified in place of public, protected or private, then the scenario is called as the package-private or default. This will mean that only the classes under the same package will be able to call the method.
package con.test.testing;
public class Test {
private string format = "";
public static void main(String[] args) {
system.out.println("Hellow Word!");
}
Protected String getformat(){
return this.format;
}
public void setformat(String format){
this.format = format;
}
}
3). The keyword, static typically means that the method belongs to the class and not just an instance of a class (object). All the static methods have to be called making use of the name of the class: “ExampleClass.methodExample ( ).“ If static as a keyword is not there, then the method can only be invoked through an object. E.g., if the class was called ExampleObject and is also had a constructor, then a new object could be made by typing ExampleObject obj = new ExampleObject ( ); and method will be called with “obj.methodExample ( .”
package con.test.testing;
class ExampleClass {
static int j=0;
static void methodExample() {
j++;
system.out.println ("value of static variable J is" + j);
}
}
Class TestStatic {
public static void main (String[] args){
ExampleClass.methodExample();
}
}
4). Void is the last word mentioned before the method, and it basically means that the method does not return anything. If you want that the method should return something, then you have to replace the word void with some data type or some object which you want to be returned. Once replaced, you will have to add return plus an object of that type towards the end of the code of the method.
Read: How to Start a Career as a Java Developer or Programmer
package con.test.testing;
class ExampleClass {
static int j=0;
static void methodExample() {
j++;
system.out.println ("value of static variable J is" + j);
}
}
Class TestStatic {
public static void main (String[] args){
ExampleClass.methodExample();
}
}
5). When you call a method which returns something, then you can make use of what has been returned. E.g., in case a someMethod ( ) returns an integer, the latter can be further be set to what it returns with “int a = some Method ( ).”
package con.test.testing;
public class testMax{
public static void main(String[] args) {
int i = 5;
int j = 2;
int k = max(i, j);
system.out.printIn("The Maximum Between" + i + "and" + j + "is" + k);
}
public static int max (int num1, int num2) {
int result;
inf (numb1 > num2)
result = num1;
else
result = num2;
return result;
}
}
6). There are some methods which need a parameter. E.g., if a method requires a parameter of an integer, it will be someMethod (int a). Thus, when you use the latter you will have to punch in the name of the method followed by the integer in the parentheses: someMethod (5) or even someMethod (n) if n is the designated integer.
package con.test.testing;
public class testInt{
public static void main(String[] args) {
int w = 5;
int s = computeSquare(a);
system.out.printIn("Square root is" + s);
}
public static int computeSquare (int x) {
int sq;
sq = (int) Math.sqrt(x);
returen sq;
}
}
7). Methods can also come with more than one parameter, which is simply separated by commas. E.g., if the method someMethod requires two parameters, int a and Object obj, it will seem like “someMethod (int a, Object obj).” For making use of this method, it will be called by the name of the method which is followed by an integer followed by an Object in parenthesis: someMethod (4, thing) where a thing is an object.
package con.test.testing;
public class testInt{
public static void main(String[] args) {
int w = 5;
int h = 2;
int a = computearea(w, h);
system.out.printIn("area is" + a);
}
public static int computeArea (int width, int height) {
int area;
area = width * height;
returen area;
}
}
Conclusion
Read: What are the Benefits of loading jQuery from CDN
The methods are basically used to club the code together in a way that it can perform a particular task with ease and perfection. There are some components of the method which are needed to be used as per their rules. Java is a highly useful programming language with many features because of which it is used in multiple industries. There are many concepts of Java which you should possibly master one at a time for a thorough understanding of each as superfluous information will lead to patchy information and faulty programming.
A dynamic, highly professional, and a global online training course provider committed to propelling the next generation of technology learners with a whole new way of training experience.
Cyber Security
QA
Salesforce
Business Analyst
MS SQL Server
Data Science
DevOps
Hadoop
Python
Artificial Intelligence
Machine Learning
Tableau
Search Posts
Related Posts
Receive Latest Materials and Offers on Java Course
Interviews