forum

Home / DeveloperSection / Forums / Java Variables and Method

Java Variables and Method

Nitin Kushwaha 437 10-Jul-2019

Java Variables:-

Variables are used for storing the data values.

Various types of variables are:

  •  String
  •  int
  •  float
  • char
  •  Boolean

Declaration of Variables:-

Type variable = value;

Example

String name = “Nitin”;

System.out.println (name);
To Top

A parameter used in Java Program:-

  • class keyword used to declare a class in java.2
  • The public keyword is used to access inside the class outside the class inside the method anywhere.
  •  static is used when we declare any method as static. There is no need to create an object to call the static method.
  •  void means that it does not contain any value.
  •  main represents the starting point of the program.
  •  String args [] is used for line command.
  • System.out.println is used to print the statement.
  •  Private Java private keyword is an access modifier. It is used to indicate that a method or variable may be accessed only in the class in which it is declared

Example’

class Student
{
public static void main (String args [])
{
System.out.println (“I Love you”);
}
}

Compile by-java Student.java

Run by- Student Java

Output;
I Love You.

Java Methods:-

  • A method is a block of code which only runs when it is called.
  • You can pass data, known as parameters, into a method.
  • Methods are also known as a function.
  • If we define the code once and use it many times.
  • The method followed by parenthesis ().
  • Java provides some pre-defined method such as System.out.println (). But we can also create own method.

Example;

public class Student
{
static void Mymethod()
{
System.out.println(“I am fine”);
}
public static void main(String args[])
{
Mymethod();
}
}
Output:-
I am fine.



Can you answer this question?


Answer

0 Answers

Liked By