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;
	}
}

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()

Ok, thank you very much.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.