# Passing an Object

#### Ryan$Joined Dec 14, 2018 178 I'm primarily programming in c /c++ and using sometimes libraries which already written in java. I have faced something like this Book b= new Book(); for me it's understandable and I know what new Book() mean, what I don't understand is, how an object stored to one variable b? I mean the object might be several memory blocks locations right? so when we write new Book() it's allocating memory location, and what's returning to b? the start address of the object? or it returns the *(start address of the first object) to b and then(analog to c/c++) I write "b." and not "b->" ? what I've understood from java that object means something "concrete" in memory, like kind of data .. ** I know there's no * and pointers in java, I'm just making a visualizing to understand the issue very well because I program in c/c++ ** Another question, will the class itself occupy memory in RAM(not meaning on its objects)? thanks in advance. Last edited: #### 402DF855 Joined Feb 9, 2013 171 Book *b = new Book; // dynamically allocated Book bb; // stack allocated, disappears when function exits I think a class ordinarily will not occupy memory unless it has static members which do not belong to an instance of the class (has no this pointer) but belongs to the class itself. #### Ian Rogers Joined Dec 12, 2012 699 I'm not sure I have ever liked the word "object"... Some numpty always comes on with "It's like a real world object" ie.. concrete as you have just said.. Its a variable!! In another thread we talked about structures... The struct is the fore runner to objects... Objects are structures and the functions that operate on those structures bundled into a single variable... This gives more structured code as each structure can be treated as one single entity!!! Book myBook = new(Book); Book is a structure.. BUT! the member variables and functions are managed... Simply a function to turn a page will hide the page index variable from you to a point you just type myBook.turnpage(); and the structure looks after itself..( assuming that is the function ).. Java has pointers they are just not as apparent as other languages... The only time a class takes up ram is when you initialise them... They have constructors to make an instance from the definition.. #### 402DF855 Joined Feb 9, 2013 171 C: Book b= new Book(); is wrong. "b" must be declared as a pointer! C: Book *b= new Book(); Book b2; Book is a class; "b" is a pointer to an object of type (class) Book. "b2" is an object of type (class) Book. "&b2" is a pointer to an object of type (class) Book. An object is a region of storage. #### WBahn Joined Mar 31, 2012 25,223 I'm primarily programming in c /c++ and using sometimes libraries which already written in java. I have faced something like this Book b= new Book(); for me it's understandable and I know what new Book() mean, what I don't understand is, how an object stored to one variable b? I mean the object might be several memory blocks locations right? so when we write new Book() it's allocating memory location, and what's returning to b? the start address of the object? or it returns the *(start address of the first object) to b and then(analog to c/c++) I write "b." and not "b->" ? what I've understood from java that object means something "concrete" in memory, like kind of data .. ** I know there's no * and pointers in java, I'm just making a visualizing to understand the issue very well because I program in c/c++ ** Another question, will the class itself occupy memory in RAM(not meaning on its objects)? thanks in advance. An object, like a structure or like an array, is always a single contiguous block of memory and its address is always the address of the first byte in the block. How classes are implemented is implementation dependent -- the compiler writer only has to make the behavior conform to the language standard. The class functions and methods are often stored just like any other functions. Some classes have data that belong to the entire class (called static in Java, don't know about C++) and so that requires memory even if there are no objects of that class. In fact, a class doesn't have to be able to instantiate objects at all -- it may only exist in order to provide static methods and data. Many math classes are like that. If you want to get a good feel for how much of this is done, from the bottom up, then work your way through the NAND-to-Tetris project. #### bogosort Joined Sep 24, 2011 441 I'm primarily programming in c /c++ and using sometimes libraries which already written in java. I have faced something like this Book b= new Book(); for me it's understandable and I know what new Book() mean, what I don't understand is, how an object stored to one variable b? I mean the object might be several memory blocks locations right? so when we write new Book() it's allocating memory location, and what's returning to b? the start address of the object? or it returns the *(start address of the first object) to b and then(analog to c/c++) I write "b." and not "b->" ? In Java, the "b" variable in your example is a reference to a newly-created Book object. References act very much like pointers in C: they point to a memory location. The statement "Book b2 = b;" would create another reference to the same object -- note that the object is not copied, just the reference. Likewise, calling some method with "b" as its argument passes the reference to the object, not the entire object. Another question, will the class itself occupy memory in RAM(not meaning on its objects)? I'm not by an means an expert on Java internals, but I would certainly think so. Objects are created at runtime, and so would need runtime access to the class definition. Furthermore, Java allows class variables (declared as static within the class), which belong to the class and not any particular instance of the class. If your Book class had a class variable named "totalBooks", you could (and should) access it without an object reference, e.g.: Book.totalBooks++;. Thread Starter #### Ryan$

Joined Dec 14, 2018
178
thanks to you all!
I have another question which I will not open a new thread for it; in java at main method why it's not allowed to write there a method which doing for me something like printing? For example
Code:
Public class main
{
Public int main()
{
Public void displayMessage()
{
System.out.println("hello");
}
}
}
Why I need to do a new class in order to declare there the function displayMessage? I can inside main itself doing that and the pc will print hello world .. I dont know where's the problem with that? .. I know about objects on java .. But why cant immediately in main class write a function and simple pc will read it and execute it without doing another class for ..

Thanks you guys very much!

#### WBahn

Joined Mar 31, 2012
25,223
What "another class"? Your code above has one class.

Please take at least enough time to TRY to present code that is SOMEWHAT close to being valid and then present it here reasonably formatted.

1) The "public" keyword has to be all lower case.
2) You can't define a method within a method (what you are calling "functions" are called "methods" in Java).
3) Since main() is defined to return a value of type int, it must return a value of type int.
4) Java generally expects the main() method to be type void and to take one argument which is an array of String objects.

Java:
public class main
{
static public void main(String args[])
{
System.out.println("hello");
}
}