Home > OS >  How to call a variable of another method in another method in same class
How to call a variable of another method in another method in same class

Time:11-19

// default packages
import java. util.*;
import java. lang.*;
import java.io.*;
import java.net.*;
import java. awt.*;
import java. applet.*;

class msg {
    void data() {
        String name, dep, age, bdg, hob;
        System.out.println("\nStudent data .....:)");
        System.out.print("\nEnter your name:");
        Scanner a = new Scanner(System.in);
        name = a.nextLine();
        System.out.print("\nEnter your department:");
        Scanner b = new Scanner(System.in);
        dep = b.nextLine();
        System.out.print("\nEnter your age:");
        Scanner c = new Scanner(System.in);
        age = c.nextLine();
        System.out.print("\nEnter your blood-group:");
        Scanner d = new Scanner(System.in);
        bdg = d.nextLine();
        System.out.print("\nEnter your hobbies:");
        Scanner e = new Scanner(System.in);
        hob = e.nextLine();
    }

    void showdata() {
        System.out.println("\nStudent data inserted successfully .....:)");
        System.out.println("Name :"   name);
        System.out.println("Department :" dep);
        System.out.println("Age :" age);
        System.out.println("Blood-Group :" bdg);
        System.out.println("Hobbies :" hob);
    }
}

public class display {
    public static void main(String[] args) {
        msg d=new msg();
        d.data();
        d.showdata();
        System.out.println("Welcome to java development !!!");
    }
}

How do I call these variables – > name, dep, age, bdg, hob – > inside the method showdata() and print that details, is there any option to call another variable of another method in a different method but in the same class



Output:
defaultjava.java:32: error: cannot find symbol
     System.out.println("Name:" name);
     ^
  symbol:   variable name
  location: class msg

CodePudding user response:

Posting OPs solution since they're unable to do it themselves.

The solution is to make the variables in question members of the class by defining them outside of any methods:

// default packages
import java.util.*;
import java.lang.*;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.applet.*;

class msg {
    String name, dep, age, bdg, hob;
    void data() {
        System.out.println("\nStudent data .....:)");
        System.out.print("\nEnter your name:");
        Scanner a = new Scanner(System.in);
        name = a.nextLine();
        System.out.print("\nEnter your department:");
        Scanner b = new Scanner(System.in);
        dep = b.nextLine();
        System.out.print("\nEnter your age:");
        Scanner c = new Scanner(System.in);
        age = c.nextLine();
        System.out.print("\nEnter your blood-group:");
        Scanner d = new Scanner(System.in);
        bdg = d.nextLine();
        System.out.print("\nEnter your hobbies:");
        Scanner e = new Scanner(System.in);
        hob = e.nextLine();
    }

    void showdata() {
        System.out.println("\nStudent data inserted successfully .....:)");
        System.out.println("Name :"   name);
        System.out.println("Department :" dep);
        System.out.println("Age :" age);
        System.out.println("Blood-Group :" bdg);
        System.out.println("Hobbies :" hob);
    }
}

public class display {
    public static void main(String[] args) {
        msg d=new msg();
        d.data();
        d.showdata();
        System.out.println("Welcome to java development !!!");
    }
}
  • Related