Results 1 to 4 of 4
  1. #1
    javanew is offline Member
    Join Date
    Mar 2010
    Posts
    49
    Rep Power
    0

    Question Please explain to me this basics in java !! array delaration

    Java Code:
    public class Driver {
    	 
    	    int[] a;
    	    int lastItem;
    	 
    	public static void main(String []args){
    	
    
    		a[0] = 4; // THIS DOES NOT WORK ?? WHY ?? PLEASE EXPLAIN:confused:....
    		
    		
    	
    		for(int s = 0 ; s < a.length; s++){
    		System.out.println(" "+a[s] );}
    		
    		
    		
    	}
    	
    }
    Last edited by Eranga; 09-06-2010 at 06:33 AM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Your problems are several-fold. For one, the variable a is declared:
    Java Code:
    int[] a;
    but never initialized
    Java Code:
    a = new int[5];
    For another, a has been declared as a non-static class field, but you're trying to access it from within a static method, and it simply doesn't exist by itself within a static method. It only exists in association with the context of a Driver object.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Go through the comments I've added in the following code.

    Java Code:
    public class Driver {
    	 
           int[] a;
           // A variable is declared. [] stands for the declaration of an array. But not initialize. That means in an array you've to define how many items you want to store in advance, before use.
    
           int lastItem;
    
    	 
           public static void main(String []args){
           a[0] = 4; // THIS DOES NOT WORK ?? WHY ?? PLEASE EXPLAIN:confused:....
    
           // In above line you found an error because the first element, a[0] is the first element of an array(items are zero based indexing), could not found by your compiler. Because compiler dosen't know how many items there have actually, in simple word.
    		
           for(int s = 0 ; s < a.length; s++){
                  System.out.println(" "+a[s] );}
    	}
    }
    If you are confusing with my comments let me know.

Similar Threads

  1. What loop? Java Networking Basics
    By JonnySnip3r in forum Networking
    Replies: 0
    Last Post: 01-28-2010, 09:37 PM
  2. Explain how to troubleshoot simple java pg
    By senthil in forum Advanced Java
    Replies: 2
    Last Post: 09-01-2009, 02:28 PM
  3. Please explain Java
    By MarkWilson in forum New To Java
    Replies: 7
    Last Post: 07-02-2008, 09:38 AM
  4. Iam new in Java Please explain to me
    By vinaytvijayan in forum AWT / Swing
    Replies: 1
    Last Post: 12-30-2007, 12:35 PM

Posting Permissions

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