Mr.Roy
Mr.Roy

Reputation: 35

how to print instance variable in methods in java?

I am trying to print the variable in show() method. But in every time it is showing default value like for string NULL and for integer 0. I took a parameterize constructor. As I need to pass the argument at the time of creating an object of the class instead of that I want to pass any variable which will carry the value of that variable from the user, but I am not able to perform.

import java.util.*;

public class Cons_With_Arg {
    String s;
    int i;
    // Scanner sc=new Scanner(System.in);

    Cons_With_Arg(String name, int id) {
        Scanner sc = new Scanner(System.in);
        this.s = name;
        System.out.print("enter Name:");
        name = sc.nextLine();
        this.i = id;
        System.out.print("enter id:");
        id = sc.nextInt();
    }

    public void show() {
        System.out.println("Name:" + this.s);
        System.out.println("Id:" + this.i);
    }

    public static void main(String[] args) {
        Cons_With_Arg co = new Cons_With_Arg(s, i);
        // System.out.println("Name:" + co.s);
        // System.out.println("Id:" + co.i);
        co.show();
    }
}

Upvotes: 1

Views: 3583

Answers (3)

Somesh Gupta
Somesh Gupta

Reputation: 41

First take the user input in the main method and then pass those using constructor. Then in the constructor set those values to instance variables using this keyword (As you already did).

import java.util.*;

public class app {
    String s;
    int i;

    app(String name, int id) {
        this.s = name;
        this.i = id;
    }

    public void show() {
        System.out.println("Name:" + this.s);
        System.out.println("Id:" + this.i);
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("enter Name:");
        String s = sc.nextLine();

        System.out.print("enter id:");
        int i = sc.nextInt();

        app co = new app(s, i);
        
        co.show();
    }
}

Upvotes: 0

Bor Laze
Bor Laze

Reputation: 2506

You shouldn't read values in constructor.

First read them

public static void main(String[] args) {
    String s;
    int i;

    Scanner sc = new Scanner(System.in);
    System.out.print("enter Name:");
    s = sc.nextLine();

    System.out.print("enter id:");
    i = sc.nextInt();

    Cons_With_Arg co = new Cons_With_Arg(s, i);
    
    co.show();
}

and then use it in your class constructor

Cons_With_Arg(String name, int id) {
    this.s = name;
    this.i = id;
}

Now all works:

enter Name:a
enter id:1
Name:a
Id:1

Upvotes: 0

Eranda Kodagoda
Eranda Kodagoda

Reputation: 23

You are assigning values to s and i before you assign them through scanner.

 test(String name, int id) {
        Scanner sc = new Scanner(System.in);

        System.out.print("enter Name:");
        name = sc.nextLine();
        s = name;

        System.out.print("enter id:");
        id = sc.nextInt();
        i = id;
    }

Upvotes: 1

Related Questions