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
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
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
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.
🌐
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.
🌐
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.
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.
🌐
Study.com
study.com › courses › 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.
🌐
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.
🌐
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
🌐
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 ...
🌐
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
🌐
Quora
quora.com › How-do-you-create-an-instance-of-a-class-in-Java
How to create an instance of a class in Java - Quora
Answer (1 of 7): So, in order to instantiate the class, all you need to do is to use the new keyword and call the constructor of that class. You can store this instance in a variable or as we call it instance variable which acts as a reference ...
🌐
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 ...
Top answer
1 of 8
24
"A constructor is the method that you will run when you instantiate the class before the object is returned to the caller. Again, instantiation happens when you create the new instance of the class using the new keyword. This is a good place to stick information about the initial setup of the object." Is it that that's confusing you? OK. So, we have a class which is a blueprint for creating instances of that class. Doing this is called instantiation, it is also referred to as 'creating an object', 'making an instance' etc. There's lots of terminology that can be used. Let's worry about what an object or instance is. An object or instance is a unique version of the class, it is an example of what the class creates, as a blueprint or template. It is the real thing rather than the template itself. Example required .. erm ... let's say you've got a machine that stamps metal into plates. So, the shape of that stamp is like the class. It isn't a plate but it knows how to create one. The object/instance is an actual plate that gets created when the stamp presses the sheet metal. There can be many, many individual plates produced from this one stamp and they can be stamped out of different thicknesses of metal; that depends what sheet metal you put against the template, but they are all, basically, made the same way. OK - that's one example lamely overdone. We now have some idea what an instance is and that means we know that instantiate is the verb used to describe creating an instance. So, what's this constructor? Like Craig said, it is a method. That's like a bit of code that does something rather than a bit of code that stores something. A method produces something. A constructor is a specific bit of code that every class has and what it does is build the instance/object. Let's get back to plates ... like that example isn't old already! In our plate factory, the constructor is the process that gets some metal, puts it in the right place then moves the stamp to press out a plate-shaped object. Craig mentioned that "This is a good place to stick information about the initial setup of the object". So, our constructor could select what type of metal to use (I dunno ... copper, tin, steel, whatever!) and its thickness, perhaps (thin, thick, something else(?!)), then create a plate by applying the template to the metal The end result is a plate that matches the "initial setup of the object" delivered to whoever requested it to be made. Or, as Craig put it, "returned to the caller". Let's make this even less clear by trying to create some dummy code for our dodgy plate-making example ... ```java public class Plate{ private int thickness; private Metal madeOf; // This is the constructor: public Plate(int howThick, Metal useMetal){ thickness = howThick; madeOf = useMetal; } } Plate myPlate = new Plate(2, copper) ``` (Yes, I made up a Metal class - forgive me!) This is probably pointless as Craig's course will do a far better job of explaining all this, but it's worth a shot. Here we've replicated the lame plate example in (probably incorrect) Java code - it isn't supposed to be perfect! The person wanting the plate, "the caller", can pass details of what type of plate he wants, "initial setup", into the constructor which will create a plate-shaped object, "instantiate", and give that to the user, "return to the caller". We can see how, in code, we can create unique objects with differing properties but by using the same template. We could add more properties; color, diameter, shape etc. I didn't as I've done the example to death already. But the key concepts here are the stamp itself - it makes plates, as many plates as you want - and the constructor which allows you to tailor the properties of your plate(s). We've covered the class, methods, the constructor, instances & objects, instantiation, the caller and we've learned I know nothing about the manufacture of plates, or any other form of crockery, for that matter. But I hope that has shed a little light on the part of the video that was causing you a problem. If not, that's my fault; sorry! Honestly, just crack on with the courses, you will get the hang of all this in no time. Have a great day! Steve. cc. @Craig Dennis (https://teamtreehouse.com/craigsdennis) - I may have confused one of your students ... sorry! :smile: :wink:
2 of 8
0
thanks steve , your explanation helped me a lot , i was really having hard times getting these concepts , i think it begins to make sense now , still feeling a little overwhelmed though but i hope it gets better as i move forward in course ... thanks again ..