The Artima Developer Community
Sponsored Link

Java Answers Forum
How to make a class instance ?

1 reply on 1 page. Most recent reply: Mar 9, 2002 7:44 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Avin Sinanan

Posts: 28
Nickname: avin
Registered: Feb, 2002

How to make a class instance ? Posted: Mar 9, 2002 1:46 PM
Reply to this message Reply
Advertisement
Hello,
Ok you must be wondering if am crazy cause am asking how to make an istance of a class. Ok let me first point out I know how to make an instance of a class :).

However I have an unsual situation.
Consider the short code below

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
 
class Test
{
	public static void main(String[] args)
	{  	
		
		Link link1 = new Link();
		Link link2 = new Link();
		
		for(int i = 0 ; i<100 ; i++)
		{
			link1.method1(i);
		}
		
		for(int j = 1 ; j<3 ; j++)
		{
			link2.method1(j);
		}
		
		
	}
}
 
 
class Link
{
	int number1 = 0 ;
	
	public Link()
	{
	}
	
	public void method1(int number)
	{
		number1 = number1 + number ;
		System.out.println(number1);
	}
}



now the code does what it is suppose to do. However I would like to get rid of the lines

Link link1 = new Link() ;
Link link2 = new Link() ;

because the user has to put in these lines. Suppose I had a thousand for loops! Then I would have to write :

Link link1 = new Link() ;
Link link2 = new Link() ;
Link link3 = new Link() ;
Link link4 = new Link() ;
and so on..

So what am trying to do is that for each for loop the program creates its own name for a Link instance. The code I have given you is a very simplified version of a program am working on.

You see in my program I have a class that is required to make an instance of a paricular class at random times in the program. Hence the user cannot write the names of each instance becasue the does not know how many instances the program will need as the creation of an instance is random. So i need the program itself to make a new class everytime it needs a new instance. And each instance had to have its own unique name.

Can anyone give me some ideas or code please.

Thanks a mllion.

Yours repesctufully Avin Sinanan


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: How to make a class instance ? Posted: Mar 9, 2002 7:44 PM
Reply to this message Reply
Sounds like you want to keep a List of instances of your class (probably an ArrayList would suffice). As you create each new instance, you just add it to the list. Also, you can add a name instance variable to your class and a constructor that lets you set the name. You could optionally have getName() and setName() methods.

Roughly, something along these lines:
class Link
{
    int number1 = 0;
    String name = "anonymous";
    public Link(String newName) 
    {
        name = newName;
    }
    
    /**
     * Customize this to your liking!
     */
    public String toString()
    {
        return name + " with value of " + number1;
    }
    public void setName(String newName)
    {
        
        // TODO: Check newName for null, if so throw an exception!
        name = newName;
    }
    public String getName()
    {
        return name;
    }
    public void method1(int number)
    {
        number1 += number;
        System.out.println(number1);
    }
}

Flat View: This topic has 1 reply on 1 page
Topic: Dynamic type casting Previous Topic   Next Topic Topic: Variables

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use