Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Java
  4. Object parsing non-static

Object parsing non-static

Scheduled Pinned Locked Moved Java
questionjavajson
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Joshua Waring
    wrote on last edited by
    #1

    From my understanding so far from the Java tutorials, but there was a question I wanted to clarify. When creation of a Object which isn't static, each decleration of the object with Object object = new Object() will create a new instance, if it was static wouldn't that mean instead of creating multiple instances, instead of only existing as one instance. As a non static would they then be independent of each other?

    public class One{
    Object object = new Object();

    public static void main(String\[\] args){
    One.start(object);
    }
    

    }
    public class Two{
    Object obj = new Object();
    public void start(Object obj){
    this.obj = obj;
    }
    }

    N J 2 Replies Last reply
    0
    • J Joshua Waring

      From my understanding so far from the Java tutorials, but there was a question I wanted to clarify. When creation of a Object which isn't static, each decleration of the object with Object object = new Object() will create a new instance, if it was static wouldn't that mean instead of creating multiple instances, instead of only existing as one instance. As a non static would they then be independent of each other?

      public class One{
      Object object = new Object();

      public static void main(String\[\] args){
      One.start(object);
      }
      

      }
      public class Two{
      Object obj = new Object();
      public void start(Object obj){
      this.obj = obj;
      }
      }

      N Offline
      N Offline
      Nagy Vilmos
      wrote on last edited by
      #2

      Your understanding is a bit off. There is no such thing, in Java at least, as a static object. There are classes with static only methods, so no instances are required, and there are singletons, classes which only ever have a single instance. Each class defines a type of object and how it works, it is not the object itself. Within a class, you can have items defined as static. Static members are shared by all instances of the class and do not actually require an instance to be created; a good example is the main entry method.


      Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

      1 Reply Last reply
      0
      • J Joshua Waring

        From my understanding so far from the Java tutorials, but there was a question I wanted to clarify. When creation of a Object which isn't static, each decleration of the object with Object object = new Object() will create a new instance, if it was static wouldn't that mean instead of creating multiple instances, instead of only existing as one instance. As a non static would they then be independent of each other?

        public class One{
        Object object = new Object();

        public static void main(String\[\] args){
        One.start(object);
        }
        

        }
        public class Two{
        Object obj = new Object();
        public void start(Object obj){
        this.obj = obj;
        }
        }

        J Offline
        J Offline
        jschell
        wrote on last edited by
        #3

        Joshhua123 wrote:

        When creation of a Object which isn't static, each decleration of the object with Object object = new Object() will create a new instance,

        You can have static and non-static member variables. If you have non static member variable as per your example then for each instance of 'One' that you create then the member variable for that instance will point to a different instance of Object.

        Joshhua123 wrote:

        if it was static wouldn't that mean instead of creating multiple instances, instead of only existing as one instance.

        If the member variable was static then there would be only one instance of Object regardless of how many new instances of 'One' you created.

        Joshhua123 wrote:

        As a non static would they then be independent of each other?

        As noted above (when it is a non-static member) there is a new Object created each time. They are different so yes they are independent.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups