Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: array element declaring problem

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default array element declaring problem

    <code>public class mHandler
    {


    public static int num=4;


    ...............
    ...........
    .........
    ......
    public static void main(String[] args)
    {


    some code}



    }


    public class A

    {
    int num;
    B aList[]=new B[num];



    }





    public class B
    {

    int x;
    int y;

    public B(int a,int b)
    {
    this.x=a;
    this.y=b;


    }

    }







    </code>


    although i have defined num as global variable, i can not define the arrays using num in the array element brackets like [num] in the class A.. Specifically aList[num] is not defined
    Last edited by jack_nutt; July 29th, 2011 at 09:22 PM.


  2. #2
    Junior Member
    Join Date
    Jul 2011
    Location
    Canada
    Posts
    11
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: array element declaring problem

    Perhaps the 'num' in the nested class is being confused with 'num' in mHandler. 'num' in your nested class (class A) is not defined. Try changing it to int num = 4;

  3. #3
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: array element declaring problem

    i know, but how can i define num once such that i dont have to change it in the other calsses by declaring its value?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: array element declaring problem

    but how can i define num once
    To define the variable num only once, remove the "int num" line:
    public class A
     
    {
    int num; // Remove this line it is a second definition of num

    Please edit your code and wrap it in code tags. Unformatted code is hard to read. See: http://www.javaprogrammingforums.com...do=bbcode#code

    If you get compile errors, please copy and paste here the full text.
    Last edited by Norm; July 30th, 2011 at 06:55 AM.

  5. #5
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: array element declaring problem

     
     
    public class mHand {
     
    	/**
    	 * @param args
    	 */public static int num =3;
    	public static void main(String[] args) {
     
    	b m=new b();
    	c g=new c();
    	m.aList[0]=g;
     
    	System.out.println(m.aList[0].x);
     
     
    		// TODO Auto-generated method stub
     
    	}
     
    }
     
     
    public class b {
    int x,y;
     
    	c aList[]=new c[num];
     
     
     
    }                    
     
     
    public class c {
     
     
    	int x=2;;
    	int y=3;
     
    }


    error come on line :
    c aList[]=new c[num];

    num cannot be resolved to a variable
    Last edited by jack_nutt; July 31st, 2011 at 10:27 PM.

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: array element declaring problem

    You have declared num in class mHand. Class B does not know anything about it. Think about different rooms in a building. I'm standing in one room with a number written on a piece of paper. You are in a different room at the other end of the building. Can you see what is written on my piece of paper?
    Improving the world one idiot at a time!

  7. #7
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: array element declaring problem

    how can i make num visible in the other class?

  8. #8
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: array element declaring problem

    If you have declared it static (which is a bad idea in my opinion) you would access it classname.variablename.

    If you make it non-static then class b would need to instantiate an object of mHand and then access it directly or via a method.
    Improving the world one idiot at a time!

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: array element declaring problem

    mHand could pass the value of num to b in b's constructor

  10. #10
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: array element declaring problem

     
    public class mHand {
     
    	/**
    	 * @param args
    	 */public static int num =3;
    	public static void main(String[] args) {
     
    	b m=new b(num);
    	c g=new c();
    	m.aList[0]=g;
     
    	System.out.println(m.aList[0].x);
     
     
    		// TODO Auto-generated method stub
     
    	}
     
    }
     
     
     
     
    public class b {
    int x,y;
    	int num;
    	c aList[]=new c[num];
     
    	public b(int num)
    	{
    		this.num=num;
     
    	}
     
    }
     
     
    public class c {
     
     
    	int x=2;;
    	int y=3;
     
    }


    even i declare num in b's constructor the aList is not created



    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at mHand.main(mHand.java:11)


    >> m.aList[0]=g;

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: array element declaring problem

    ArrayIndexOutOfBoundsException: 0
    The array does not have 1 element (the 0th)
    Where do you give a size/number of dimensions to the aList array? Is the size value > 0?

  12. #12
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: array element declaring problem

    Try creating the array after you assign a value to num.
    Improving the world one idiot at a time!

Similar Threads

  1. URGENT!!!! help with array element comparing
    By Neo in forum Java Theory & Questions
    Replies: 3
    Last Post: March 3rd, 2011, 08:52 AM
  2. problem searching for a String element in a parallel array
    By david185000 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: July 27th, 2010, 01:24 PM
  3. Taking an element out of an array
    By SlckP in forum Collections and Generics
    Replies: 3
    Last Post: May 5th, 2010, 02:26 PM
  4. how to find an element in an array
    By humdinger in forum Collections and Generics
    Replies: 8
    Last Post: April 9th, 2010, 05:22 PM
  5. [SOLVED] theory behind testing each element of an array.
    By etidd in forum Java Theory & Questions
    Replies: 2
    Last Post: February 5th, 2010, 09:04 AM