LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 01-24-2012, 08:31 PM   #1
halfpower
Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 194

Rep: Reputation: 30
Post Java program behaving strangely


I wrote a java program and it is not behaving as expecting. This is the code:
Code:
public class test{    
    static MyPoint[] p = new MyPoint[3];  
      
    public static void printPoints(){    
        System.out.println( " x = " + p[0].x + " y = " + p[0].y);
        System.out.println( " x = " + p[1].x + " y = " + p[1].y);
        System.out.println( " x = " + p[2].x + " y = " + p[2].y);
    }
       
    public static void main(String[] args) {
        p[0].x = 0.0f;
        p[0].y = 0.0f;        
        p[1].x = 0.12f;
        p[1].y = 0.13f;        
        p[2].x = 0.367878f;
        p[2].y = 0.756785f;        
        printPoints();
    }      
}

class MyPoint{    
    public static float x;
    public static float y;    
}
I would expect the output of this program to be:
Code:
 x = 0.0 y = 0.0
 x = 0.12 y = 0.13
 x = 0.367878 y = 0.756785
But instead, the output is:
Code:
 x = 0.367878 y = 0.756785
 x = 0.367878 y = 0.756785
 x = 0.367878 y = 0.756785
I'm pulling my hair out trying to figure out why this is happening. Does anyone know what it is I'm overlooking?
 
Old 01-24-2012, 10:28 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 1,421

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Code:
class MyPoint{    
    public static float x;
    public static float y;    
}
static means there will be a single copy shared among all objects of the class.
 
Old 01-25-2012, 12:04 PM   #3
halfpower
Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 194

Original Poster
Rep: Reputation: 30
Won't that be okay? Won't "static MyPoint[] p = new MyPoint[3]" create separate instances of MyPoint?
 
Old 01-25-2012, 12:31 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 1,421

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Quote:
Originally Posted by halfpower View Post
Won't "static MyPoint[] p = new MyPoint[3]" create separate instances of MyPoint?
Yes, but

Code:
class MyPoint{    
    public static float x;
    public static float y;    
}
means each MyPoint instance will use the same x and y.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
aptitude: behaving strangely jdkaye Debian 2 02-15-2009 09:23 AM
Broadcom w/l behaving strangely lately. algogeek Linux - Hardware 1 06-15-2008 11:36 PM
Grub behaving strangely live_dont_exist Linux - General 2 08-06-2006 02:04 PM
Second X Server behaving (at least to me) strangely exitsfunnel Linux - Software 2 05-13-2005 05:52 PM
ATI Drivers (or something) behaving strangely... doxxan Linux - Hardware 3 02-19-2004 02:37 PM


All times are GMT -5. The time now is 04:43 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration