How do I create an instance of a class in a method using Java?

  • Context: Java 
  • Thread starter Thread starter Robben
  • Start date Start date
  • Tags Tags
    Class Java Method
Click For Summary

Discussion Overview

The discussion revolves around creating an instance of a class within a method in Java, specifically focusing on the challenges faced when dealing with abstract classes and constructors. The scope includes homework-related queries and technical explanations regarding object instantiation.

Discussion Character

  • Homework-related
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • One participant expresses confusion about creating an instance of an abstract class, particularly in relation to methods that require an instance to perform actions.
  • Another participant suggests using the Java reflection API to create instances, referencing the need for the Constructor.newInstance() method due to multiple parameters in the constructor.
  • A different participant clarifies that abstract classes cannot be instantiated directly and emphasizes the need to derive a concrete subclass before instantiation.

Areas of Agreement / Disagreement

There is no consensus on the best approach to instantiate an abstract class, as participants present differing views on the use of reflection and the necessity of subclassing.

Contextual Notes

Participants have not fully resolved the implications of using reflection for instantiation, nor have they clarified the specific parameters required for the constructor in the abstract class.

Robben
Messages
166
Reaction score
2

Homework Statement



How do I create an instance of a class in a method?

Homework Equations



None

The Attempt at a Solution



I am a bit rusty whenever I think of instances. I always think of main method and objects when I see instance which gets me confused on what to do with the following example:

I have a abstract class, School, and inside School I have some methods that must preform some action on an instance. For example, there is a move() method that must move the instance of School. Another method named, personOld(), which returns whether or not an instance of School surpassed some determined age.
How do I create this instance?
 
Last edited:
Technology news on Phys.org

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 25 ·
Replies
25
Views
6K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 3 ·
Replies
3
Views
7K
  • · Replies 23 ·
Replies
23
Views
3K
Replies
2
Views
3K
  • · Replies 14 ·
Replies
14
Views
6K
Replies
3
Views
4K
  • · Replies 3 ·
Replies
3
Views
1K