There is no Sample class in your code . The one which you have declared is a private method .
// private method which takes an int as parameter and returns another int
private int Sample(int c)
{
int a = 1;
int b = 2;
c = a + b;
return c;
}
With the current snippet , You need to instantiate the Testing class and make use of the Sample method. Notice your class definition is preceded by the keyword class , in this case class Testing.
public class Testing{
private int Sample(int c)
{
int a = 1;
int b = 2;
c = a + b;
return c;
}
public static void main(String []args)
{
Testing t = new Testing(); // instantiate a Testing class object
int result = t.Sample(1); // use the instance t to invoke a method on it
System.out.println(result);
}
}
But that doesn't really make sense, your Sample method always returns 3 .
Are you trying to do something like this :
class Sample {
int a;
int b;
Sample(int a, int b) {
this.a = a;
this.b = b;
}
public int sum() {
return a + b;
}
}
public class Testing {
public static void main(String[] args) {
Sample myTest = new Sample(1, 2);
int sum = myTest.sum();
System.out.println(sum);
}
}
Answer from AllTooSir on Stack OverflowThere is no Sample class in your code . The one which you have declared is a private method .
// private method which takes an int as parameter and returns another int
private int Sample(int c)
{
int a = 1;
int b = 2;
c = a + b;
return c;
}
With the current snippet , You need to instantiate the Testing class and make use of the Sample method. Notice your class definition is preceded by the keyword class , in this case class Testing.
public class Testing{
private int Sample(int c)
{
int a = 1;
int b = 2;
c = a + b;
return c;
}
public static void main(String []args)
{
Testing t = new Testing(); // instantiate a Testing class object
int result = t.Sample(1); // use the instance t to invoke a method on it
System.out.println(result);
}
}
But that doesn't really make sense, your Sample method always returns 3 .
Are you trying to do something like this :
class Sample {
int a;
int b;
Sample(int a, int b) {
this.a = a;
this.b = b;
}
public int sum() {
return a + b;
}
}
public class Testing {
public static void main(String[] args) {
Sample myTest = new Sample(1, 2);
int sum = myTest.sum();
System.out.println(sum);
}
}
I doubt you actually want to create an object.
From your code snippet, I understand that you want to run a 'method' named Sample which adds two numbers. And in JAVA you don't have to instantiate methods. Objects are instances of class. A method is just a behavior which this class has.
For your requirement, you don't need to explicitly instantiate anything as when you run the compiled code JAVA automatically creates an instance of your class and looks for main() method in it to execute.
Probably you want to just do following:
public class Testing{
private int sample(int a, int b) {
return a + b;
}
public static void main(String[] args) {
int c = sample(1, 2);
System.out.println(c);
}
}
Note: I changed Sample to sample as it's generally accepted practice to start a method name with lower-case and class name with an upper-case letter, so Testing is correct on that front.
java - Can I Instantiate a class using the class object? What about Constructors? - Stack Overflow
Help me to understand what instantiating means - Java
java - What does it mean to instantiate a class? - Stack Overflow
string - Is there a way to instantiate a class by name in Java? - Stack Overflow
Videos
In Java 9 and afterward, if there's a declared zero-parameter ("nullary") constructor, you'd use Class.getDeclaredConstructor() to get it, then call newInstance() on it:
Object foo(Class type) throws InstantiationException, IllegalAccessException, InvocationTargetException {
return type.getDeclaredConstructor().newInstance();
}
Prior to Java 9, you would have used Class.newInstance:
Object foo(Class type) throws InstantiationException, IllegalAccessException {
return type.newInstance();
}
...but it was deprecated as of Java 9 because it threw any exception thrown by the constructor, even checked exceptions, but didn't (of course) declare those checked exceptions, effectively bypassing compile-time checked exception handling. Constructor.newInstance wraps exceptions from the constructor in InvocationTargetException instead.
Both of the above assume there's a zero-parameter constructor. A more robust route is to go through Class.getDeclaredConstructors or Class.getConstructors, which takes you into using the Reflection stuff in the java.lang.reflect package, to find a constructor with the parameter types matching the arguments you intend to give it.
Use:
type.newInstance()
For creating an instance using the empty costructor, or use the method type.getConstructor(..) to get the relevant constructor and then invoke it.
You can use Class.getConstructors (or Class.getConstructor) to get a list of available constructors, and invoke any of them with Constructor.newInstance, which does accept parameters.
Just to add one point I see missing:
You can invoke newInstance directly on the Class object if it has a public null constructor. (Null constructor is the constructor with no arguments.)
Otherwise, you can find constructors via Class.getConstructors() as others have said.
First correct me if I am wrong.
Instantiating just means creating an instance of a class, right?
If I have a class Pets, and I create a new pet object - this means I just instantiated, right? I created an instance, in this case an object, of the class Pets.
Can I instantiate other things other than objects? Does that mean when I call a method from Pets class, that means I created an instance of that Class, in this case a method?
If my above is correct, my deeper question is below.
I can't understand when it says,
Let's first create the superclass. Note the usage of abstract keyword in class definition. This marks the class to be abstract, which means it can not be instantiated directly."
This is from the site, https://javatutorial.net/java-abstraction-example, a couple paragraphs down.
You can instantiate a class? How?
Two ways:
Method 1 - only for classes having a no-arg constructor
If your class has a no-arg constructor, you can get a Class object using Class.forName() and use the newInstance() method to create an instance (though beware that this method is often considered evil because it can defeat Java's checked exceptions).
For example:
Class<?> clazz = Class.forName("java.util.Date");
Object date = clazz.newInstance();
Method 2
An alternative safer approach which also works if the class doesn't have any no-arg constructors is to query your class object to get its Constructor object and call a newInstance() method on this object:
Class<?> clazz = Class.forName("com.foo.MyClass");
Constructor<?> constructor = clazz.getConstructor(String.class, Integer.class);
Object instance = constructor.newInstance("stringparam", 42);
Both methods are known as reflection. You will typically have to catch the various exceptions which can occur, including things like:
- the JVM can't find or can't load your class
- the class you're trying to instantiate doesn't have the right sort of constructors
- the constructor itself threw an exception
- the constructor you're trying to invoke isn't public
- a security manager has been installed and is preventing reflection from occurring
MyClass myInstance = (MyClass) Class.forName("MyClass").newInstance();
Both are correct and both mean the exact same thing. Objects are instances of classes. Whether you "instantiate class X [to create an object of class X]" or "instantiate an object [of class X]" is just semantic nitpicking.
Is it more accurate to say "instantiate a class" or "instantiate an object"?
Webster defines instantiate:
to represent (an abstraction) by a concrete instance
From this definition, we could use either "instantiate a class" or "instantiate an object." We could also simply say, "instantiate" since "class," (the abstraction) and, "object," (the concrete instance) are both now made redundant by the definition of instantiate.
If we wanted to be more precise, we could specify the object we're instantiating, for example: "instantiate a Foo."
Background information:
This chart demonstrates that the word "instantiate" did not come into common use until starting in the 1950s, and peaking very recently. Source.

Further, this ngram demonstrates that "instantiate an object" is much more common than "instantiate a class".
