Instance created inside of a Method

how to pass value from one method to another in java
method inside method java example
calling a method within a method java
how to call one method from another method in same class in java
how to call a method in java
how to create a method inside a class in java
methods in java
can we write a method inside main method in java

I have not been able to find any reliable literature on this but i'm curious as to where an object is stored if its created inside of a method ? on the stack or on the heap in java 8?

public class A {}
.
.
.
public class B { 
  public void test(){
   A m = new A();
  }
}

I know that normally only local primitives, reference variables and function calls are stored on the stack and that objects are stored in the heap

So I'm assuming that one of the following two scenarios is true either

  • Case 1 : Objects instantiated in a method are as usual stored in the heap with a reference to that object in the stack then when the function finishes the object reference goes out of scope and the object in the heap is then available for garbage collection
  • Case 2 : Objects instantiated in a method are stored in the stack then are available for garbage collection when function finishes

I strongly suspect its case 1, it wouldn't make sense to store an object in a stack, the only reason I have doubts is because ive come across some literature saying on the stack and others on the heap

Thanks for your feedback ahead of time

The local reference variable is on the stack, and the object is in the heap.

Note that your question title,

Instance declared inside of a Method

is misleading since objects/instances are declared nowhere -- only variables are, and the object created in a method can be placed on fields into a collection, or anywhere it's needed. And so there is no guarantee that the object should be GC'd when the method exits.

Method within method in java, What happens when you call a method on an object in Python? A: Method parameters are virtually the same as local variables—they’re declared inside the method (well, technically they’re declared in the argument list of the method rather than within the body of the method, but they’re still local variables as opposed to instance variables).

All the objects are stored in the heap ... the garbage collector runs whenever there's a lack of memory so it get rid of the objects that are not used anymore and there's no reference to them too.

When is it OK to create object of a class inside a method of that class , An anonymous inner class can be useful when making an instance of an object with certain “extras” A class created inside a method is called local inner class. Instance method(s) belong to the Object of the class not to the class i.e. they can be called after creating the Object of the class. Every individual Object created from the class has its own copy of the instance method(s) of that class. Instance methods are not stored on a per-instance basis, even with virtual methods.

Your assumption specified in Case 1 is correct. Here is a good source on memory allocation in java.

Java Heap Space vs Stack – Memory Allocation in Java

Can a class make instances of itself?, It is not mandatory that you have to define main method inside another class within the same Instances are created by using the `new` keyword on that class​. Dear experts, Kindly guide me how to call an instance method inside a other class method. i am already using the static method from same class. Pls refer the code CALL METHOD /sapsrm/cl_pdo_msg =>/ sapsrm/if_pdo_msg_provider ~ add_message_stati

If they are not immediate "values" (not like an int property in a class object), only their reference values are stored in the stack. their actual values and structures are stored in the heap.

these are stored in the stack as they are;

int i=10;
float f=10.00;
bool b=true;

these will have only references on the stack, they will reside in the heap (any kind of class variable is part of the structure and be created on the heap altogether);

int[] ii={1,2,3};
double[] dd = new double[10];
String s="String!!";
Object o=new Object();

when passed to a method, Values are copied to a new stack variable (unless converted to a reference object). References are also passed to a copycat stack variable, but since they are references, both will redirect to the same object (unless copied to a whole new object manually).


this part might not be of interest in the topic, you decide

In the following code, number is created on the stack and copied to a new stack value for the method, hello created on the heap, passed by reference to s then two strings concatenated on another heap address and it now holds this new address (strings are immutable). The Point object is not immutable as strings, so it can be changed anywhere you referenced it as they are created on the heap to access freely.

class Point{ int x;int y;  Point(int x,int y){this.x=x;this.y=y;} }

public class TestClass{
     public static void main(String []args){
        int number=5;
        String hello="Hello";
        Point point = new Point(2,4);
        Print(number,hello,point);
        System.out.println(hello+" "+number+" "+point.x+" "+point.y);
     }
     public static void Print(int i,String s,Point p){
        i+=5;
        s+="World!";
        p.x+=2; p.y+=2;
        System.out.println(s+" "+i+" "+p.x+" "+p.y);
     }
}

What happens when you call a method on an object in Python?, Every method is declared with a return type, but until now we've made all of our Instance variables are declared inside a class but not within a method. Instance Methods. The first method on MyClass, called method, is a regular instance method. That’s the basic, no-frills method type you’ll use most of the time. You can see the method takes one parameter, self, which points to an instance of MyClass when the method is called (but of course instance methods can accept more than just one parameter).

In Java, can I create the object of a class inside the same class itself , Here we've just used a Function Expression to create the function and assign it to the property user. For instance, the code inside user. To define a function outside the class definition, you have to declare it inside the class and then define it outside of the class. This is done by specifiying the name of the class, followed the scope resolution :: operator, followed by the name of the function:

4. Methods Use Instance Variables: How Objects Behave, Instance methods alter their object variable while static functions return the value that gets "shipped out", so to speak, with each instance of that object you create​. and then storing the result which is a "new PVector" inside of a variable. Instance variables, owned by objects of the class, allow for each object or instance to have different values assigned to those variables. Working with Class and Instance Variables Together. Class variables and instance variables will often be utilized at the same time, so let’s look at an example of this using the Shark class we created. The comments in the program outline each step of the process.

Object methods, "this", Instance variables are created when an object is instantiated, and are accessible to all the methods, the constructor and block in the class. Access modifiers can be​  Methods can be either instance or static. Invoking an instance method requires that you instantiate an object and call the method on that object; an instance method operates on that instance and its data. You invoke a static method by referencing the name of the type to which the method belongs; static methods do not operate on instance data.

Comments
  • Object instances are conceptually stored in the heap. How the JVM implementation really works is completely up to the JVM itself. A JVM is free to store them on punchcards and reread them every time they're needed, as long as it looks from the Java code as if they're on the heap. In some cases, the current oracle JVM actually replaces you object allocation with a stack allocation of all its fields, but it first ensures that it only does that when your application cannot notice the difference.
  • haha you're right I changed , thanks for the quick response I suspected case 1 was right I just wanted to make sure
  • Minor syntactic point: while objects aren't declared inside a method, classes can be: docs.oracle.com/javase/tutorial/java/javaOO/localclasses.html