Results 1 to 12 of 12
  1. #1
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    6

    Default Are classes and methods stored in memory or are they just lines of code?

    I understand that classes are kinda instructions/lines of code. Their instances are real entities which exist/are stored in memory.

    I understand that methods are just lines of code, and the data they manipulate is the real entities which are actually stored in memory.

    Static variables are the ones which exist as only one copy in memory, which is available to every instance of the class. Instance variables are the ones for which each instance of the class has its own copy.

    What about methods when considered in this context? Assuming that just like variables, instance methods also exist as separate copies for each of the instances of the class, what difference does it really make? The method is going to execute the same set of instructions whether it is static or nonstatic.

    Java Code:
    int static staticSum(int a, int b) {
      return a+b;
    }
    and

    Java Code:
    int nonStaticSum(int a, int b) {
      return a+b;
    }
    I understand that both these methods will be called using different syntax, and they access the same data in different ways (e.g. static method accesses instance variables using dot notation(with an object's name) and access static variables directly)... but apart from the syntactical differences, what is the difference in semantics (if I am using the term correctly)? What difference does it ultimately make when even all copies of an instance method are going to execute exactly the same set of instructions?

  2. #2
    Pavlo is offline Member
    Join Date
    Dec 2012
    Posts
    28
    Rep Power
    0

    Default Re: Are classes and methods stored in memory or are they just lines of code?

    Hello.

    Looks like you asked few questions, but they are appears because you not know some fundamentals.

    Consider java application compiled classes are stored in computer memory (RAM) at application run time. Java applications run in java virtual machine (JVM), so JVM allocates RAM of your PC, and java applications are placed in JVM memory. This topic is much deeper, and if you fill necessary - study it more with search keywords: "Stack Heap Java"

    Next about static method and instance methods. Instance method can access instance variable, so inside of class instance method you can access any of current object property. But static method cannot do this. Inside static method body, instances inner properties are not visible, because static context knows nothing about class instances. It only can access static property variables. I'm sure you have to read about this to understand basics.

    Start with static variables and methods. Think it is valuable because main method is static :) . And just after it look deeper in Java memory organization.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Are classes and methods stored in memory or are they just lines of code?

    Quote Originally Posted by Zarah View Post
    Assuming that just like variables, instance methods also exist as separate copies for each of the instances of the class, what difference does it really make?
    That's probably your key misconception.
    Each instance of a class does not get its own copy of the code.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    6

    Default Re: Are classes and methods stored in memory or are they just lines of code?

    That's probably your key misconception.
    Each instance of a class does not get its own copy of the code.
    Yes, thank you. But now I am wondering why did they create separate method categories i.e. static methods and non-static methods, why didn't they just design methods inwhich static fields would be accessed by class' name and nonstatic fields would be accessed by instance's name?

    After all, both static and nonstatic methods are just executable blocks of code - and the same lines of code are executed no matter what context the method is invoked in (static or nonstatic).

    From oracle's tutorial,

    A common use for static methods is to access static fields.
    But static fields can also be accessed by nonstatic methods.

    What is the use of static methods which instance methods can't fulfill? and what is the use of instance methods which static methods can't fulfill?
    Last edited by Zarah; 05-17-2016 at 08:59 PM.

  5. #5
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    6

    Default Re: Are classes and methods stored in memory or are they just lines of code?

    Next about static method and instance methods. Instance method can access instance variable, so inside of class instance method you can access any of current object property. But static method cannot do this. Inside static method body, instances inner properties are not visible
    But static methods can access any object's properties by using the dot notation, isn't it? So they can access an object's properties, the difference is just in the syntax, no?

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Are classes and methods stored in memory or are they just lines of code?

    For functional methods that don't need to access instance fields. use static (like the Math class). The advantage is that you don't
    need to instantiate a class to use them. And of course, static methods cannot be overridden.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Are classes and methods stored in memory or are they just lines of code?

    Maybe some ancient C++ terminology can help you out here: an object (an instantiation of a class) is a struct that contains the member variables of the object. Each non-static method gets a hidden first parameter passed which is a reference (a pointer) to such a struct. The name of the parameter is 'this'. So the non static method can refer to its member variables as 'this.variable'; static methods don't get this hidden first parameter passed to it when called. The compiler allows you to omit the explicit 'this' reference when no ambiguity occurs.

    Java does the same thing ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Are classes and methods stored in memory or are they just lines of code?

    Quote Originally Posted by Zarah View Post
    Yes, thank you. But now I am wondering why did they create separate method categories i.e. static methods and non-static methods, why didn't they just design methods inwhich static fields would be accessed by class' name and nonstatic fields would be accessed by instance's name?
    Because there are many methods that do not require an instance of a class (that is require any instance data) to work.
    Just take a look at the Math class. It makes no sense to create a Math instance just to run the max() method.

    The thing about using static methods to access static field (non-constant ones) is the static equivalent of getters and setters for those fields. Think of the getInstance method of a singleton.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    6

    Default Re: Are classes and methods stored in memory or are they just lines of code?

    Maybe some ancient C++ terminology can help you out here: an object (an instantiation of a class) is a struct that contains the member variables of the object. Each non-static method gets a hidden first parameter passed which is a reference (a pointer) to such a struct. The name of the parameter is 'this'. So the non static method can refer to its member variables as 'this.variable'; static methods don't get this hidden first parameter passed to it when called. The compiler allows you to omit the explicit 'this' reference when no ambiguity occurs.

    Java does the same thing ...
    Yes, it's quite helpful. Thank you so very much.

  10. #10
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    6

    Default Re: Are classes and methods stored in memory or are they just lines of code?

    @Jim829 @JosAH @Tolls

    I got it now. Thank you so, so very much indeed.

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Are classes and methods stored in memory or are they just lines of code?

    Quote Originally Posted by Zarah View Post
    @Jim829 @JosAH> @Tolls

    I got it now. Thank you so, so very much indeed.
    Decent amounts of tobacco, Ketel 1 Jenever and espresso coffee are considered a proper award; the others don't need/want anything ;-)

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  12. #12
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    6

    Default Re: Are classes and methods stored in memory or are they just lines of code?

    Decent amounts of tobacco, Ketel 1 Jenever and espresso coffee are considered a proper award; the others don't need/want anything ;-)

    kind regards,

    Jos
    Well if there wasn't a distance of thousands of miles involved, you would definitely get an espresso. =)

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. Replies: 11
    Last Post: 01-14-2012, 06:46 PM
  3. Calling methods on objects stored in an array
    By Dreaming in forum New To Java
    Replies: 6
    Last Post: 10-30-2011, 12:50 AM
  4. Source code of Predefined classes and methods
    By lobodelbosque in forum New To Java
    Replies: 8
    Last Post: 08-10-2011, 02:11 PM
  5. Replies: 2
    Last Post: 08-21-2008, 07:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •