---
title: "Java Variables and Method"  
description: "Java Variables and Method"  
author: "Nitin Kushwaha"  
published: 2019-07-10  
updated: 2025-03-16  
canonical: https://www.mindstick.com/forum/55154/java-variables-and-method  
category: "java"  
tags: ["variables are used for storing the data values."]  
reading_time: 3 minutes  

---

# Java Variables and Method

## Java [Variables](https://www.mindstick.com/articles/715/php-variables):-

Variables are used for storing the data values.

Various types of variables are:

- String
- int
- float
- char
- Boolean

## Declaration of Variables:-

Type [variable](https://www.mindstick.com/articles/1807/objective-c-data-types-variables-object-creation) = value;

Example

```
String name = “Nitin”;
```

```
System.out.println (name);
```

[To Top](https://www.mindstick.com/Forum/Submit#)

## A [parameter](https://www.mindstick.com/blog/450/parameter-class-in-c-sharp) 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](https://www.mindstick.com/interview/2468/can-you-access-the-private-method-from-outside-the-class) inside the method anywhere.
- static is used when we declare any [method as](https://www.mindstick.com/interview/2388/can-you-declare-the-main-method-as-final-in-java) static. There is no need to create an object to call the [static method](https://www.mindstick.com/blog/440/creating-static-methods-in-c-sharp).
- 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](https://www.mindstick.com/forum/351/how-to-pass-data-from-controller-to-ascx-page), [known as](https://answers.mindstick.com/qa/35703/ricky-ponting-is-also-known-as-what) [parameters](https://www.mindstick.com/articles/87/passing-parameters-in-c-sharp), 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.
```

\

## Replies

### Reply by Khushi Singh

The Java programming language uses variables together with methods as its core tools to establish program behavior as well as overall functionality. A program becomes modular and reusable due to the combination of storage through variables and operation execution through methods which act on stored data.

[Java programming](https://www.mindstick.com/articles/167915/top-5-emerging-technologies-among-java-developers) includes variables that operate as labeled memory locations which link to particular data types. Java provides three variable types including local variables for method declarations and local scope memory allocation and instance variables as object-related elements inside class definitions outside method declarations and static variables for class-level variables requiring the static keyword declaration. Java offers two types of data variables including basic primitive elements such as int, double and boolean alongside reference elements consisting of classes and arrays.

Java programming language provides methods as distinct blocks of code where developers can implement particular functional tasks. Java includes built-in methods for output printing as `System.out.println()` together with methods that developers implement to develop logical encapsulation. All methods contain one or more defined return types together with a mandatory method name but may optionally include parameters. A method absent from returning anything must declare itself using the void keyword.

Effective use of method declarations alongside variables within Java programs creates better code structures that minimize multiple statements and simplify upkeep. Programming methods allow developers to achieve code reuse and variables provide efficient storage and manipulation functions for data.


---

Original Source: https://www.mindstick.com/forum/55154/java-variables-and-method

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
