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 5 of 5

Thread: Array of objects, invoking constructor for one changes others

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Array of objects, invoking constructor for one changes others

    Hi all, first post here, I'm having some trouble on a critical part of a program I'm writing for a programming class I'm currently taking. The entire program is pretty large (10+ classes, some well over 100 lines) so I won't post the whole thing here, but here's the part where I'm stuck...

    I have this class IPaddress, which is fairly simple. Basically, the constructor takes in a string in format "xxx.xxx.xxx.xxx" (for example, "192.168.1.255"). Then it splits it up into four shorts in an array using StringTokenizer, and stores them and is able to output each "field" separately (that's what those get methods are).

    import java.util.StringTokenizer;
    public class IPaddress 
    {
    	private static short[] address = new short[4];
    	private static final byte FIELD_A = 0, FIELD_B = 1, FIELD_C = 2, FIELD_D = 3;
    	public IPaddress(String input)
    	{
    		StringTokenizer theTokenizer = new StringTokenizer(input, ".");
    		for(byte i = 0; i < 4; i++)
    			address[i] = Short.parseShort(theTokenizer.nextToken());
    	}
    	public short getFieldA()
    	{
    		return(address[FIELD_A]);
    	}
    	public short getFieldB()
    	{
    		return(address[FIELD_B]);
    	}
    	public short getFieldC()
    	{
    		return(address[FIELD_C]);
    	}
    	public short getFieldD()
    	{
    		return(address[FIELD_D]);
    	}
    }

    In another class, I have these two static methods, ReadLogData and ProcessLine (ReadLogData I invoke and ProcessLine is invoked by the first method). RedLogData is passed in the string of a file path, and then chops up the file by line and processes each line to get the string format I mentioned above. Then it returns an array of IPaddress objects.

    public class WebLogData 
    {
    	private static int numberOfHits;
    	private static final int DEFAULT_SIZE = 20000;
    	public static String ProcessLine(String theLine) //take in single line
    	{
    		StringTokenizer theTokenizer = new StringTokenizer(theLine, " "); //string tokenizer with delimiter
    		return(theTokenizer.nextToken()); //takes first part of line and discards the rest
     
    	}
    	public static IPaddress[] ReadLogData(String theFileName) //take in file name
    	{
    		IPaddress[] addressList = new IPaddress[DEFAULT_SIZE]; //array of IPaddress objects
    		try
    		{
    			File theFile = new File(theFileName); //file reader
    			Scanner theScanner = new Scanner(theFile); //scanner
    			for(numberOfHits = 0; theScanner.hasNext(); numberOfHits++) //run while there are more lines
    				addressList[numberOfHits] = new IPaddress(ProcessLine(theScanner.nextLine())); //element object IPaddress gets its constructor invoked
    			theScanner.close(); //close scanner
    		}
    		catch(FileNotFoundException e) //catch exception
    		{
    			e.printStackTrace();
    		}
    		return(addressList);
    	}
    	public static int GetNumberOfHits()
    	{
    		return(numberOfHits);
    	}
    }

    Now the problem: when I invoke the constructor addressList[numberOfHits] = new IPaddress(ProcessLine(theScsanner.nextLine())), it doesn't just construct an object IPaddress at addressList[numberOfHits], it constructs an object there and then modifies all previous objects in the array that have been constructed! So not just the object at addressList[numberOfHits], but the object at addressList[numberOfHits - 1], addressList[numberOfHits - 2], etc. This is screwing up my whole program! I can't continue debugging until I get this fixed

    Thanks in advance for any help.
    ~Matt


  2. #2
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    489
    Thanks
    8
    Thanked 45 Times in 43 Posts

    Default Re: Array of objects, invoking constructor for one changes others

    Do you know the meaning in Java of the keyword static?

    db

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array of objects, invoking constructor for one changes others

    Static methods are invoked using the class name instead of the object/instance name.

    Of course if I'm doing it wrong, please tell me
    Last edited by BigFoot13; October 24th, 2010 at 12:48 PM.
    ~Matt

  4. #4
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,099
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: Array of objects, invoking constructor for one changes others

    In layman's terms, using static variables in your class IPAddress means 'you change one, you change them all'. See Understanding Instance and Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

  5. #5
    Junior Member
    Join Date
    Oct 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array of objects, invoking constructor for one changes others

    Ahh I see! Removed the static modifier from my short[] address variable and its working now! Thanks!!!
    ~Matt

Similar Threads

  1. Object array with constructor
    By thedolphin13 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 8th, 2010, 11:02 AM
  2. problem reading from two files into array objects
    By JavaRTnoob in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 30th, 2010, 09:21 AM
  3. FAQ: find variable in an array of objects
    By dalek in forum Object Oriented Programming
    Replies: 1
    Last Post: April 5th, 2010, 12:40 PM
  4. Passing objects as a constructor parameter
    By derky in forum Object Oriented Programming
    Replies: 2
    Last Post: October 27th, 2009, 04:31 AM
  5. [SOLVED] Creation of objects of Array in Java
    By sadi_shihab in forum Collections and Generics
    Replies: 4
    Last Post: July 9th, 2009, 01:38 PM