Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2004

Tip: Looking for answers? Try searching our database.

Confused with java classes

Thread view: 
Junior - 10 Mar 2004 04:40 GMT
hi,

I am just start learning java and i got this problem
I tried to create a class called testing and in the main file i create
2 object classes, but they are always linked togeter.
any one can help me ?

thanks
Jerry

public class testing
{
   private  static String internal[][];
   public testing()
   {
       internal = new String[100][3];
   }
    public testing(int one)
   {
       internal = new String[100][3];
   }
   public void add()
   {
       internal[0][0] = "Me me me and me";
       internal[1][0] = "Me me me and me";
       internal[2][0] = "Me me me and me";
       internal[3][0] = "Me me me and me";
   }
   public void minus()
   {
       internal[0][0] = "Mdme";
       internal[1][0] = "Mwe";
       internal[2][0] = "Mde";
       internal[3][0] = "Md";
   }
    public static void displayTable()
    {
           int row;
           int rowNow = 4;
           
           for (row = 0; row < rowNow; row ++)
        {
         
            System.out.println (internal[row][0]);
        }
    }

}
---main class
testing label1 = new testing();
testing label2 = new testing(1);
label1.add();
label1.displayTable();
System.out.println("----------------");
label2.displayTable();

---the result:
Me me me and me
Me me me and me
Me me me and me
Me me me and me
----------------
Me me me and me
Me me me and me
Me me me and me
Me me me and me
Andrew Thompson - 10 Mar 2004 05:12 GMT
> I am just start learning java and i got this problem
> I tried to create a class called testing and in the main file i create
> 2 object classes, but they are always linked togeter.
> any one can help me ?

Uggh..
http://www.catb.org/~esr/faqs/smart-questions.html#prune

Try my variant and see if it makes
it more clear.  Note that it now
includes a main, and has a more
sensible name that follows the
Java naming convention (class names
start with capital letters)

<code>
public class ArrayTest
{
   private static String internal[][];

   public ArrayTest()
   {
       internal = new String[100][3];
   }

   public ArrayTest(int one)
   {
       internal = new String[100][3];
   }
   public void add()
   {
       internal[0][0] = "Me me me and me";
       internal[1][0] = "Me me me and me";
       internal[2][0] = "Me me me and me";
       internal[3][0] = "Me me me and me";
   }
   public void minus()
   {
       internal[0][0] = "Mdme";
       internal[1][0] = "Mwe";
       internal[2][0] = "Mde";
       internal[3][0] = "Md";
   }
    public static void displayTable()
    {
           int row;
           int rowNow = 4;

           for (row = 0; row < rowNow; row ++)
        {

            System.out.println (internal[row][0]);
        }
    }

    public static void main(String args[])
    {
        ArrayTest at1 = new ArrayTest();
        at1.add();
        at1.displayTable();

        ArrayTest at2 = new ArrayTest();
        at2.minus();
        at2.displayTable();
    }

}
</code>

Signature

Andrew Thompson
* http://www.PhySci.org/ Open-source software suite
* http://www.PhySci.org/codes/ Web & IT Help
* http://www.1point1C.org/ Science & Technology

FISH - 10 Mar 2004 18:15 GMT
> hi,
>
[quoted text clipped - 9 lines]
> {
>     private  static String internal[][];
              ^^^^^^

You have made the array static, meaning that it 'belongs' to the class
itself, not to each individual instance of the class.  You perhaps
imagined that each instance of your class had its own String array
called internal - but in fact the static modifier caused them to
share a single array, so changes were reflected across all instances.

Simply remove the keyword static from the array defintion, and it
should work as you expect.

Btw: consider taking the advice offered elsewhere in this thread, and
use standard Java naming conventions.

-FISH-   ><


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.