Ask a question

What Is The Difference Between Static Member Vs. Non Static Member Of An Object Java

What's the difference between a static data member and a regular data member?

For a static data member, there will only ever be one instance of that member. For a non-static data member, there is one instance for every object created.

A simple example:
class foo
int value;
static int static_value;

If you create a million foo objects, then there will be a million chunks of memory set aside to hold the value member. There will be on, and only 1 chunk of memory set aside to hold the value of static_value.
If you modify obj.value, then it only modifies the value field in 1 or your 1 million objects.
If you modify obj.static_value, then it modifies the value that every one of you 1 million objects sees.

What is the difference between a static member and an instance member (Java)?

the context of class attributes, statichas a different meaning. If you have a field like:private static int sharedAttribute;
then, each and every instance of the class will share the same variable, so that if you change it in one instance, the change will reflect in all instances, created either before or after the change.Thus said, you might understand that this is bad in many cases, because it can easiy turn into an undesired side-effect: changing object a also affects b and you might end up wondering why b changed with no apparent reasons. Anyway, there are cases where this behaviour is absolutely desirable:class constants: since they are const, having all the classes access the same value will do no harm, because no one can change that. They can save memory too, if you have a lot of instances of that class. Not sure about concurrent access, though.variables that are intended to be shared, such as reference counters &co.static vars are instantiated before your program starts, so if you have too many of them, you could slow down startup.A static method can only access staticattributes, but think twice before trying this.Rule of thumb: don't use static, unless it is necessary and you know what you are doing or you are declaring a class constant.

What is the difference between a static and a non-static method or member function in Java?

static keyword is used when a resource(variable, method) is in shareable mode i.e that resource is used by many objects. If we make a resource static then only single copy of that resource is created for all the objects. We can make a variable ,method ,block and inner class as static. The static variable are associated with class. Static resources get initialized when class is loaded in the method area in memory area by JVM.static method has scope at class level they will get initialized when class is loaded by JVM in method area. Whereas non static method get memory at later stage after the creation of an object. They are concerned with the instance(object) of the class. This is the reason why we can’t declare or call a non static method inside a static method.

Difference between member function and non member function in c++ ?

Member function is a function that is defined inside a class defenition.

Member function can use ALL [no matter whether it is private public or protected] the functions declared IN IT'S RESPECTIVE CLASS independently ( i mean without any object and dot operator) whereas a non member function cannot access all the members of a class, only those that are declared public.

one more thing : member functions declared INSIDE the class definition (without scope resolution operator) behave as INLINE functions whereas if they are declared outside the class using Scope Resolution operator, they behave as ordinary functions

Difference between static and instance methods (java)?

Instance members belong to a single instance of a class. Even instance gets a copy of the member that's local to them. Instance variables in particular are also called fields. And instance-nested classes are also called "inner" classes.

Static members belong to the entire class. Every instance of the class shares the same member. Static members are also called class members.

Use instance members when the scope of a property or method belongs to a specific instance. For example:
- It makes sense that a point's coordinates only belong to it.
- A method to move that point only really makes sense if it also belongs to that point instance.

Use static members when you need to access the member without an instance of the class. For example:
- Java constants often need to be static.
- Factory methods and builder classes need to be static (since they create or fetch a new instance of the class they belong to.)
- Copys of methods that are provided so that an instance doesn't need to be created to do a calculation, e.g: You might have a Polygon.getArea() instance method that calculate the area for a given polygon instance, and a static Polygon.getArea(Point...) method that calculates the area for a polygon made of that series of points, but without creating a point.

There is technically no main class. It's the nickname that the class with the public, static, void method main(String[] args) is typically referred to when it's ran. There can be multiple classes with that method, so only the one being ran by Java is called the main one.

That said, that class can be a full class with members, like a constructor, fields and every thing.

For example, you could have a point class that has the main(String[] args) method:
    public class Point {

        public double x = 0;
        public double y = 0;

        public String toString() {
            return "(" + x + "," + y + ")";

        public static void main(String[] args) {
            Point origin = new Point();
            System.out.println("The point of origin is represented by " + origin);


This is a bad example for obvious reasons, but it's simple and proves the point. The class with main could be a fully functioning class.

What is the difference between static and non-static methods in Java?

Here is some difference about Static and non-static Method1)Class Level & Object Level :- Static method is class level method, and non-static method is object level method.2) Method Copy:- Static method is class level method therefore one copy of method is available for whole class where as non- static method have different different copy for every object.3) Method Call :- We can Call Static method directly using class name,ClassName.staticMethod();Whereas not- static method can not be called without class object.classObject.method();4) Memory Allocation:- For Static Method Memory is allocate once at the time of class loading. For non-static method memory is allocate every time whenever method gets call.To get more question answer follow me on Qura Yogesh PawarRegard,Yogesh PawarJava Programmer

What is the difference between c++ & JAVA?


There are also significant differences from C++, which fundamentally makes Java distinct from C++. Perhaps the single biggest difference between Java and C++ is that Java does not support pointers. Pointers are inherently insecure and troublesome. Since pointers do not exist in Java, neither does the -> operator. Some other C++ features are not found in Java.

1 Java does not include structures or unions because the class encompasses these other forms. It is redundant to include them.
2 Java does not support operator overloading.
3 Java does not include a preprocessor or support the preprocessor directives.
4 Java does not perform any automatic type conversions that result in a loss of precision.
5 All the code in a Java program is encapsulated within one or more classes. Therefore, Java does not have global variables or global functions.
6 Java does not support multiple inheritance.
7 Java does not support destructors, but rather, add the finalize() function.
8 Java does not have the delete operator.
9 The << and >> are not overloaded for I/O operations.
10 Java does not support templates.

What is the purpose of a static object in Java? When is it actually used or in which context?

Java Static VariablesJava instance variables are given separate memory for storage. If there is a need for a variable to be common to all the objects of a single java class, then the static modifier should be used in the variable declaration.Any java object that belongs to that class can modify its static variables.Also, an instance is not a must to modify the static variable and it can be accessed using the java class directly.Static variables can be accessed by java instance methods also.When the value of a constant is known at compile time it is declared ‘final’ using the ‘static’ keyword.Java Static MethodsSimilar to static variables, java static methods are also common to classes and not tied to a java instance.Good practice in java is that, static methods should be invoked with using the class name though it can be invoked using an object. ClassName.methodName(arguments) or objectName.methodName(arguments)General use for java static methods is to access static fields.Static methods can be accessed by java instance methods.Java static methods cannot access instance variables or instance methods directly.Java static methods cannot use the ‘this’ keyword.Java Static ClassesFor java classes, only an inner class can be declared using the static modifier.For java a static inner class it does not mean that, all their members are static. These are called nested static classes in java.

What is the difference between static class and normal class in Java? And why do we use it?

In general programming language, static class is a class which can not be instantiated i.e. we can not use "new" keyword to create an object of a static class. Hence, the members of the static class needs to be accessed by using class name directly.e.g.public static class Circle {public static double area(double radius) {return 3.14 * radius * radius;}}class Area {public static void main() {double circleArea = Circle.area(10);Console.Write(circleArea); //Output-> 314}}While a normal class/ non-static class is a class which needs "new" keyword to create it's object.e.g. Had the Circle class in above example was non-static, we would have called area method in main function as:Circle c = new Circle();double circleArea = c.area(10);Console.Write(circleArea); //Output-> 314Now considering Java, we can write static classes but in a nested approach. Java allows us to write one class in another. The class which encloses another class is called Outer class while the other is nested class. Also, if the nested class is non-static, it is called Inner class.The difference between static and non-static class are:i. Nested static class does not need reference of the outer class but nested non-static class requires it.ii. Nested static class can only access static data members and member functions of the outer class but non-static/ inner class can access both, static as well as non-static members of the outer class.iii. We cannot create instance of inner class without an instance of outer class but inner class can reference data memebers and member functions of outer class directly, so there is no need to pass the reference of an object of outer class to the constructor of inner class.When to use static and non-static classes:If you need access to the non-public members and functions of an enclosing instance, use non-static nested class and use static nested class if you don't need an access to the non-public members and functions of an enclosing instance.Happy coding!✌️