-1

In order to access a non-static members/variables of a class you need to first create an object in public static void main(String args[]) method.

Does this mean that all the objects that are created are static?

I need a clear explanation and also I need to know how the memory allocation is done for static methods/variables of a class.

I know this sounds like a naive question, but I need to know about it.

Srinivas Valekar
  • 1,083
  • 10
  • 19
  • 3
    Why would you think that objects created in the main method are static? – Daniel Aug 16 '14 at 14:21
  • Please clarify your specific problem or add additional details to highlight exactly what you need. As currently written, its hard to tell exactly what you're asking. See [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask). – DavidPostill Aug 16 '14 at 14:21
  • since public static void main is a static method,we are creating objects of a class in a static method. so I thought objects created are static – Srinivas Valekar Aug 16 '14 at 14:25

2 Answers2

2

You state this:

In order to access a non-static members/variables of a class you need to first create an object in public static void main(String args[]) method.

That is only partiallly true. Yes. You do need to create an instance of a class in order to use its non-static members. But the instance does not need to be created in the main method. Indeed, you don't necessarily need to create any objects in main. (There are other ways to create your application's "primordial" objects ... and indeed, your application may not even have a main.)

Does this mean that all the objects that are created are static?

No. For a number of reasons.

  • Your initial premise is not (entirely) true.
  • The fact that some (primordial) objects are created by static methods does not mean that all objects are created that way.
  • The most important reason is that there is no such thing as a static object in Java!

This notion1 of static applies to fields and methods. A static field or method is one that is not a member of a single object (instance). But that doesn't apply to objects (instances) themselves. In Java, objects are always independent entities, and they are never members of something else ... at least at the programming language level.

For example:

public class Example {

    public static String test;

    public static void main(String[] args) {
        test = new String("Hi mum!");
    }
}

The test variable is static and we assign a reference to a freshly created String object to it. But later on, we could assign the value of test to a non-static variable, or assign a reference to a different object to test. The object that we created is no different to one that we might create in a non-static context.

I need to know how the memory allocation is done for static methods/variables of a class.

  • The allocation of memory for objects is done exactly the same way if the method where new is invoked is static or non-static.
  • The allocation of memory for objects is done exactly the same way if the field you are assigning the reference to is static or non-static.

There are a couple of points of difference between static and non-static fields as far as memory management is concerned:

  • Static fields are garbage collection roots. This means that the objects that they point to are always2 reachable; i.e. they won't be garbage collected.

  • The static fields themselves belong to frames that are typically heap nodes. (For older JVMs they are in the permgen heap, but with Java 8 the distinction between ordinary and permgen heap has gone away.) When a class is loaded, the JVM allocates this frame and hooks it into its private class data structures.


1 - The static keyword is also used with nested class declarations, but it means something rather different there. I only mention this for "completeness".

2 - If an application or framework implements "hot loading", and you don't have "class loader leaks" then classes and their associated statics can be garbage collected. Hence, static fields are not always permanently reachable.

MC Emperor
  • 22,334
  • 15
  • 80
  • 130
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

No, they aren't. If that was true, no non-static thing could ever exist, because all (normal) Java programs start with the main(String[]) method.

Objects are never static.

You can create classes, which are kind of, let's say, blueprints of an object:

public class Aa {

    public void doSomething() { ... }

    public static void doMore() { ... }

}

You can create an instance of that class (that is, an object) like this:

new Aa();

If you run a class, the Java Virtual Machine is started and it searches for the main(String[]) method. Because there is not yet any instance of an object, the method must be declared static.

public class Main {

    public static void main(String[] args) {

        // Without an instance of Aa, you can only call static methods OF
        // THAT CLASS:
        Aa.doMore();

        // This will throw a compiler error:
        // non-static method doSomething() cannot be referenced from a
        // static context, because the method is not declared static.
        Aa.doSomething();

        Aa instanceOfAa = new Aa(); // Create a new object of type Aa.

        // Since we've created an instance of the class Aa (thus it is an
        // object), we can call non-static methods OF THAT CLASS if we call
        // the reference to it. The variable myInstanceOfAa refers to an
        // instance of class Aa, thus we can call non-static methods on it,
        // like this:
        instanceOfAa.doSomething();
    }
}

See The Java Tutorials:

Sometimes, you want to have variables that are common to all objects. This is accomplished with the static modifier. Fields that have the static modifier in their declaration are called static fields or class variables. They are associated with the class, rather than with any object. Every instance of the class shares a class variable, which is in one fixed location in memory. Any object can change the value of a class variable, but class variables can also be manipulated without creating an instance of the class.

So you don't need an instance of a class (that is, an object) in order to get it.

I suggest you read the Java Tutorials.

Memory

The memory part of your question might have been answered here.

Community
  • 1
  • 1
MC Emperor
  • 22,334
  • 15
  • 80
  • 130
  • You cannot access a non static members of a class in main method,but in order to access them you create an instance of the class.Does this mean that we are converting a class into a static one? But Java says that you cannot have a static class. It is really confusing me. Please help. – Srinivas Valekar Aug 16 '14 at 14:40
  • In order to access non-static members of a class, you must create an instance of *that class*. It doesn't matter in which method (static or non-static) you are writing. – MC Emperor Aug 16 '14 at 15:05
  • I have expanded my answer a little bit, watch the WORDS IN UPPERCASE. – MC Emperor Aug 16 '14 at 15:11