491

what is the actual difference between reference variable and object

I have been reading a lot and now i really confused. Consider an ordinary instantiation:

Sampleclass instance1 = new Sampleclass();

After reading a lot I came to know that instance1 is a reference variable stored in a stack which contains the memory address of the object's data which is stored in heap.

If this is correct then where is object? instance1 is also a object. Sometimes I have seen only declaration like new Sampleclass(). Is that sufficient for object instantiation?

Answer1:

instance1 is a variable.

Because its type is a reference type, it is a reference to an object instance that lives on the heap.

new SampleClass() is a constructor call that creates a new object on the heap and returns a reference to it.

Answer2:

instance1 contains the copy of the reference which points to the memory where new objet Sampleclass() is created. What's confusing is instance1 is mere a copy of a reference, which is different from reference ref (C# Reference), which might confuse you as it confused me.

Answer3:

The expression new Sampleclass() creates an object. It also has a value which is a pointer to that object. You can do something with this pointer such as store it in a variable (e.g. Sampleclass instance1 = new Sampleclass(); ) or you can ignore it.

Why create something and ignore it? Because its constructor might have beneficial side effects for example.

Answer4:

1) Sampleclass (Type) > Container type

2) instance1 (Identifier) > user-friendly name of reference (01010101010) of data which is stored in memory (heap) and "instance1" itself stored in the stack with reference (01010101010)

3) = (operator) > to asign left side value to right side

4) new (keyword) > Purchase a new space to stored data

5) Sampleclass(); (Constructor ) > make a copy of Type "Sampleclass" and stored in newly purchased space (this is actually an object or instance) and this accessed by its name "instance1" because "instance1" know the actual location of stored data in heap memory.

Recommend

  • C# to F# class transition
  • Mocking HTTP response with Dart
  • Serialize/Deserialize derived class as base class
  • Flash Loader and ByteArray
  • Copy columns of zoo object on the basis of a condition in data frame
  • indicateing to which interval a date belongs
  • WeakReference and memory leaks
  • Hosted IronPython: import files which contain custom functions
  • Concatenating string names of variables matlabfile in R
  • Lifetime of Option::map's argument
  • Saving java object to PostgreSQL problem
  • InternalsVisibleTo is not working for wpf application
  • Application will not launch from command line full path, but will after CDing to directory
  • Swift 1.2 assigning let after initialization
  • JS: innerHTML and DOM aren't cooperating
  • Eclipse won't start
  • How to replace one set of color 'shades' with another set
  • SessionInformation expiration doesn't lead to user log out from system
  • Proper way to add unescaped text from a field to a regex in postgres?
  • Ant: fileset “dir” attribute with a runtime expanded full path
  • Excel passing a range into a function
  • HALF_PTR Windows data type
  • What is the likely cause of a net::ERR_CONNECTION_ABORTED when uploading a file to Spring
  • What Makes These Two Array Adds Different?
  • unable to get jsonEncode in magento2
  • Servlet stops working on Tomcat server after some hits or time
  • NUnit 3.0 TestCase const custom object arguments
  • Implicit joins and Where in Doctrine - how?
  • Spring boot 2.0.0.M4 required a bean named 'entityManagerFactory' that could not be found
  • Web.config system.webserver errors
  • How can I set a binding to a Combox in a UserControl?
  • Create DicomImage from scratch using Dcmtk
  • Refering to the class itself from within a class mehod in Objective C
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • Highlight one bar in a series in highcharts?
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • Calling of Constructors in a Java
  • 0x202A in filename: Why?
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • PHP: When would you need the self:: keyword?
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Angular 2 constructor injection vs direct access
  • How do I configure my settings file to work with unit tests?
  • File not found error Google Drive API
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Binding checkboxes to object values in AngularJs
  • How to Embed XSL into XML