Results 1 to 2 of 2
  1. #1
    Shyam Singh is offline Member
    Join Date
    Jun 2008
    Posts
    48
    Rep Power
    0

    Question [SOLVED] Unable to access array defiened in constructor in other methods.

    /* taking user i/p for row & column size of an array..

    * using constructor to collect the value which i m passing from main method

    * i m type casting the i/p taken in main using Scanner class .


    * Problem -->> unable to access the stud array in other methods .

    */


    import java.io.*;
    import java.util.Scanner;

    class inuser {

    public inuser(int r,int c) {

    int stud[][] = new int[r][c];

    for(int x=0;x<r;x++) {

    for(int y=0;y<c;y++) {

    int mul=(x+1)*(y+1);
    stud[x][y]=mul;
    }
    }
    } // constructor closed

    public void dispaly(int r,int c) {

    for(int x=0;x<r;x++) {
    for(int y=0;y<c;y++) {

    System.out.print("\t"+stud[x][y]);
    }
    System.out.println("");
    }
    }

    public static void main(String args[]) throws IOException {

    System.out.println("enter row value");

    Scanner s=new Scanner(System.in);

    int r=s.nextInt();

    System.out.println("entred value ="+r);
    System.out.println("enter Column value");

    int c=s.nextInt();
    System.out.println("entred value ="+c);
    inuser in=new inuser(r,c);
    in.display(r,c);

    }
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    stud can NOT be reached from other methods because it is only defined inside the inuser constructor. Move its definition outside the constructor and the other methods will be able to see it.
    You can still do the initialization in the constructor.

Similar Threads

  1. Array Constructor
    By Javanoob828282 in forum New To Java
    Replies: 1
    Last Post: 04-30-2008, 10:25 PM
  2. Replies: 0
    Last Post: 12-19-2007, 09:10 AM
  3. Replies: 0
    Last Post: 12-19-2007, 09:01 AM
  4. Replies: 1
    Last Post: 12-14-2007, 07:25 AM
  5. How to access array stored in database through JSTL
    By saurabh.joshi in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 11-19-2007, 01:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •