Hi!

Could someone please tell me how can I create an obejct of the Class1 shown below? Let's say I want initialize the List of subjects. I try the following code, but it doesn't work:

List<ClassInterface> myInterface;
		myInterface = Class1.getAllSubjects();

Thanks!

public class Class1
{
	public List<ClassInteface> getAllSubjects()
	{
		List<ClassInteface> subjects = new ArrayList<ClassInteface>();

		try
		{
			Thread.sleep(3 * 100);
		}
		catch (InterruptedException e)
		{
			// 
		}

		subjects.add(new MySubject("Subject 1"));
		subjects.add(new MySubject("Subject 2"));
		subjects.add(new MySubject("Subject 3"));

		return subjects;
	}
}

public class MySubject implements ClassInterface
{
	private String	result		= "";
	private String	name		= "";

	public MySubject(String name)
	{
		this.name = name;
	}

	public String getName()
	{
		return name;
	}

	public String getResult()
	{
		return result;
	}
}

Edited 6 Years Ago by LianaN: n/a

The syntax for creating a new instance is

Class1 varName = new Class1();

This will call the default constructor (you haven't defined any other). Now, to access the only thing you have in the class, which is the getAllSubjects() method, you use the dot notation syntax

varName.getAllSubjects()

Edited 6 Years Ago by apines: n/a

Ok, thank you very much.

P.S.: As I see my solution is also workable in case of a static getAllSubjects().

This question has already been answered. Start a new discussion instead.