PGoCareerGoCareer prep tools
Home
LoginSign up
  • Java
  • Python
  • AI
  • React
  • Angular
  • PHP
  • Node.js
  • SQL
  • DSA
  • HTML
  • CSS
  • JS
  • Spring
  • ML
  • MongoDB

Java · Theory

Java Variables

← All stacks

Theory

10/270

Java Variables

A variable is a named box that holds a value. int age = 21; means an integer box called age with 21 inside. Java wants the type first so it knows what can go in the box.

A local variable lives only inside a method. An instance variable belongs to each object. A static variable belongs to the class and is shared by all objects. Use names like studentName, not x1.

If you use a local variable before giving it a value, Java will not compile. Give it a value first. In an interview, show one local, one instance, one static example.

Let's take this on the board with one tiny Main class — no extra files. Java Variables — output: Asha is 21. age is int, name is String — two real variables. Start from main, go line by line, and stop at each print. That output is the proof for Variables.

Exam tip

Show one local, one instance, one static. Say where each lives.

Example

public class Main {
  public static void main(String[] args) {
    int age = 21;
    String name = "Asha";
    System.out.println(name + " is " + age);
  }
}

Java Variables — output: Asha is 21. age is int, name is String — two real variables.

Short notes

  • DefA variable is a named box that holds a value. type name = value.
  • RuleLocal = inside method. Instance = each object. Static = shared by class.
  • RememberName with camelCase — studentName, not x1.
  • TrapLocal variable used before a value → compile error.

Questions

1

What is a variable?

2

Local vs instance vs static?

3

What if a local has no value?

Previous← JVM: Java Virtual MachineNextData Types in Java →
P

GoCareerGo

Utilities · Preparation Hub · Resume · CV · Tools — one workspace.

Workspace

DashboardProfilePreparation HubResume builderCV builderCareer planning

PDF Tools

Merge PDFSplit PDFCompress PDFImage to PDFAll toolsJobs

Image & QR

Compress ImageResize ImageQR ScannerQR GeneratorBlogIT interview prep

Company

FAQFeedbackContactPrivacyTermsSitemap

© 2026 GoCareerGo. Keep moving forward.