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 Overflow
🌐
Oracle
docs.oracle.com › javase › tutorial › java › javaOO › objectcreation.html
Creating Objects (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
A variable in this state, which currently references no object, can be illustrated as follows (the variable name, originOne, plus a reference pointing to nothing): The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory.
Top answer
1 of 7
18

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);
 }
}
2 of 7
4

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.

Discussions

java - Can I Instantiate a class using the class object? What about Constructors? - Stack Overflow
I am storing a list of classes through (Classname.class) and would like to instantiate one? Is this possible? newInstance seems to the method I am after but it doesn't support a constructor? More on stackoverflow.com
🌐 stackoverflow.com
Help me to understand what instantiating means - Java
You are correct in that "instantiating" a class means to make a specific instance of it; making a pet object would be an instance of your pet class. What you're quoting from the Java site is related to another concept, inheritance. An abstract class cannot be instantiated because it is allowed to define methods without actually providing any functionality in those methods. (NOTE: They can provide functionality, they just aren't required to) What you have to do is create a class of your own that implements the abstract class (the syntax of which I'm not familiar with in Java). Part of doing this will be putting the functional code in all the methods that are defined by the abstract class. Since your class is not abstract, it is required to have functionality in all of its methods so it's allowed to be instantiated. More on reddit.com
🌐 r/learnprogramming
11
6
February 13, 2020
java - What does it mean to instantiate a class? - Stack Overflow
The nsLookup class is instantiated with a string that defines the host to be queried. The constructor instantiates the InetAddress object using this string. A method is designed to resolve the lookup query. The query can return multiple IP addresses if they exist. These should be returned to the GUI as an array of String objects for display. ... import java... More on stackoverflow.com
🌐 stackoverflow.com
string - Is there a way to instantiate a class by name in Java? - Stack Overflow
I was looking as the question : Instantiate a class from its string name which describes how to instantiate a class when having its name. Is there a way to do it in Java? I will have the package name and class name and I need to be able to create an object having that particular name. More on stackoverflow.com
🌐 stackoverflow.com
🌐
IIT Kanpur
iitk.ac.in › esc101 › 05Aug › tutorial › java › data › objectcreation.html
Creating Objects
As discussed below, this is also known as instantiating a class. Initialization: The new operator is followed by a call to a constructor. For example, Point(23, 94) is a call to Point's only constructor. The constructor initializes the new object.
🌐
Scaler
scaler.com › home › topics › what is instantiation in java?
What is Instantiation in Java? - Scaler Topics
May 4, 2023 - By instantiation, we mean the act of calling a class's constructor, which creates an instance or object of that class. During this process, memory is allocated for the object, and a reference to it is returned.
🌐
Javapractices
javapractices.com › topic › TopicAction.do
Java Practices->Construct Object using class name
Construct Object using class name You can create an object dynamically at runtime using only the name of the class, input as a simple string. This is done using a part of the Java language called reflection.
Find elsewhere
🌐
Quora
quora.com › Is-it-standard-practice-to-instantiate-a-class-inside-of-a-class-in-Java
Is it standard practice to instantiate a class inside of a class in Java? - Quora
When you make a new instance from a class you do it like so: ... This creates an object of class Dog and calls the constructor on it, passing the variable for initialization.
🌐
Reddit
reddit.com › r/learnprogramming › help me to understand what instantiating means - java
r/learnprogramming on Reddit: Help me to understand what instantiating means - Java
February 13, 2020 -

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?

Top answer
1 of 5
3
You are correct in that "instantiating" a class means to make a specific instance of it; making a pet object would be an instance of your pet class. What you're quoting from the Java site is related to another concept, inheritance. An abstract class cannot be instantiated because it is allowed to define methods without actually providing any functionality in those methods. (NOTE: They can provide functionality, they just aren't required to) What you have to do is create a class of your own that implements the abstract class (the syntax of which I'm not familiar with in Java). Part of doing this will be putting the functional code in all the methods that are defined by the abstract class. Since your class is not abstract, it is required to have functionality in all of its methods so it's allowed to be instantiated.
2 of 5
2
You have probably heard that a Class is the blueprint of an object. When you instantiate a class you take that "blueprint" and actually build a real live object. If you have a blueprint for a house and you build the house, you created an instance of that blueprint. If you build a second house from that same blueprint, you created a second instance of that same blueprint. Third house, third instance, etc. This whole time you still have the same blueprint. Well, abstract classes are like blueprints that you cant actually build anything from. It describes a set of instructions that other Classes (blueprints) need to follow. If you have a diagram of how to build a cinder block foundation, you cant build a house with just that. But if you add that to the blueprint of how to build walls and a roof, suddenly you have a whole house. If you add that same cinder block foundation to a blueprint with pillars and a roof you can made a gazebo. Likewise, you can create abstract classes that describe base functionality that many different classes need. Like a log in function for a website. Having just a log in function is not enough for an entire website, but it is base functionality that is needed in many spots across the website. Many different classes can inherit from that class to allow them to implement a log in method without having to define it in that class. They "abstract" away the idea of logging in to a super class and just worry about what is specific to them. When you inherit from an abstract class you then need to implement all of its methods. That way if someone does not know about your class, but knows about the super class, they can ask for the super class, get your class, and be sure that the methods they need will be provided. Abstract classes and interfaces have a lot of similarities. At my job we use interfaces exclusively to accomplish this idea.
🌐
University of Washington
courses.cs.washington.edu › courses › cse341 › 99wi › java › tutorial › java › javaOO › objectcreation.html
Creating Objects
The new operator instantiates a class by allocating memory for a new object of that type. new requires a single argument: a call to a constructor method. Constructor methods are special methods provided by each Java class that are responsible for initializing new objects of that type.
🌐
O'Reilly
oreilly.com › library › view › javatm-phrasebook › 0768668255 › 0768668255_ch16lev1sec12.html
Loading and Instantiating a Class Dynamically - Java™ Phrasebook [Book]
November 6, 2006 - Using the Class.forName() and the newInstance() methods of a Class object, you can dynamically load and instantiate a class when you don’t know the class’s name until runtime.
Author   Timothy Fisher
Published   2006
Pages   224
🌐
Study.com
study.com › business courses › java programming tutorial & training
What is Instantiation in Java? - Definition & Example - Lesson | Study.com
May 15, 2017 - When a true employee is hired, they inherit the properties of the generic Employee class. When a new instance is created, a constructor is invoked, which tells the system to go out and grab some memory for the object and initialize variables. If no constructor is defined by the programmer, Java uses a default. However, once you define a constructor (e.g., add parameters for an Employee ID and Rate of Pay), each time a new instance is created through instantiation, that constructor is to be called.
🌐
Delft Stack
delftstack.com › home › howto › java › instanitiate objects in java
How to Instantiate an Object in Java | Delft Stack
March 11, 2025 - The most common way to instantiate an object in Java is by using the new keyword. This method is straightforward and widely used in various applications. When you declare an object using new, Java allocates memory for the object and initializes it.
🌐
Linux Hint
linuxhint.com › instantiate-object-in-java
How to Instantiate an Object in Java
April 11, 2023 - Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
🌐
Princeton
cs.princeton.edu › courses › archive › spr96 › cs333 › java › tutorial › java › anatomy › creating.html
Declaring, Instantiating and Initializing an Object
The new operator instantiates a new object by allocating memory for it. new requires a single argument: a constructor method for the object to be created. The constructor method is responsible for initializing the new object. Classes provide constructor methods to initialize a new object of ...
🌐
Micro Focus
microfocus.com › documentation › silk-performer › 205 › en › silkperformer-205-webhelp-en › SILKPERF-BBA4E0A3-INSTANTIATINGJAVAOBJECTS-CON.html
Instantiating Java Objects
The JavaLoadObject function is used to instantiate Java objects, and additionally obtain handles on Java objects. Such handles can later be used to call methods on objects or they can be used as input parameters for other method calls. For the JavaLoadObject function, you must provide fully ...