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
  1. Home
  2. General Programming
  3. Java
  4. static keyword problem.....

static keyword problem.....

Scheduled Pinned Locked Moved Java
javahelpquestion
7 Posts 6 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.
  • G Offline
    G Offline
    gateway23
    wrote on last edited by
    #1

    in java a static method can access only other static member . But,how static main method access non-static member of other class....? Please tell me... sir/mam

    T J L A 4 Replies Last reply
    0
    • G gateway23

      in java a static method can access only other static member . But,how static main method access non-static member of other class....? Please tell me... sir/mam

      T Offline
      T Offline
      TorstenH
      wrote on last edited by
      #2

      do not. That's bad style. If you need static final values, put them in an Interface:

      public interface IInterface {

      public class Def{
      public static final String
      VALUE1= "Value1",
      VALUE2 = "Value2";
      }
      }

      You can then access these values like IInterface.Def.VALUE1 EDIT: The main-class should instance a Object. Not much more to do in the main, leave the rest to the new Object:

      public static void main(String[] args) {
      new MyObject();
      }

      regards Torsten I never finish anyth...

      D 1 Reply Last reply
      0
      • T TorstenH

        do not. That's bad style. If you need static final values, put them in an Interface:

        public interface IInterface {

        public class Def{
        public static final String
        VALUE1= "Value1",
        VALUE2 = "Value2";
        }
        }

        You can then access these values like IInterface.Def.VALUE1 EDIT: The main-class should instance a Object. Not much more to do in the main, leave the rest to the new Object:

        public static void main(String[] args) {
        new MyObject();
        }

        regards Torsten I never finish anyth...

        D Offline
        D Offline
        David Skelly
        wrote on last edited by
        #3

        I don't like the style you've shown here for the main method. It means that your entire application is running within the scope of MyObject's contructor, which just always feels wrong to me. A constructor is there to construct an object and get it ready to do work, not to do the actual work. I would have two classes:

        public class MyApp {

        public static void main(String[] args) {
        MyWorker w = new MyWorker();
        w.run();
        }

        }

        public class MyWorker implements Runnable {

        public void run() {
        // doStuff
        }

        }

        That way MyWorker can be regarded as a reusable service, and the startup code is completely separate.

        T 1 Reply Last reply
        0
        • D David Skelly

          I don't like the style you've shown here for the main method. It means that your entire application is running within the scope of MyObject's contructor, which just always feels wrong to me. A constructor is there to construct an object and get it ready to do work, not to do the actual work. I would have two classes:

          public class MyApp {

          public static void main(String[] args) {
          MyWorker w = new MyWorker();
          w.run();
          }

          }

          public class MyWorker implements Runnable {

          public void run() {
          // doStuff
          }

          }

          That way MyWorker can be regarded as a reusable service, and the startup code is completely separate.

          T Offline
          T Offline
          TorstenH
          wrote on last edited by
          #4

          I agree on that - luckily now it's up to you to explain all about Runnable and Thread ;P :rolleyes:

          regards Torsten I never finish anyth...

          1 Reply Last reply
          0
          • G gateway23

            in java a static method can access only other static member . But,how static main method access non-static member of other class....? Please tell me... sir/mam

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

            If you have a non-static member variable and the access modifier of the class allows that member to be exposed then you must have an instance of the class to access it.

            1 Reply Last reply
            0
            • G gateway23

              in java a static method can access only other static member . But,how static main method access non-static member of other class....? Please tell me... sir/mam

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              A static member of a class is constrained to access only the static member of the same class. It can, however, access any non-static member (if it is accessible from outside) of any class (including the class in which it is defined) if it has an instance variable to access the non-static member.

              1 Reply Last reply
              0
              • G gateway23

                in java a static method can access only other static member . But,how static main method access non-static member of other class....? Please tell me... sir/mam

                A Offline
                A Offline
                Ashish Tyagi 40
                wrote on last edited by
                #7

                Actually non-static members are accessed via an object. An static member function can access non-static member of it's class also but through an oject... like-- class TestClass{ int i_m_belongs_to_an_object_of_TestClass; // non-static data member static int i_m_belongs_to_TestClass; // static data member public static void function(){ // i_m_belongs_to_an_object_of_TestClass = 0; // wont compile i_m_belongs_to_TestClass = 0; TestClass obj = new TestClass(); obj.i_m_belongs_to_an_object_of_TestClass = 0; // will compiled but accessed through object OtherClass objOther = new OtherClass(); objOther.i_m_belongs_to_an_object_of_OtherClass = 0; // will compiled but accessed through object int someInt = OtherClass.i_m_belongs_to_OtherClass; // accessed through Class name can also be accessed by an object } } class OtherClass { int i_m_belongs_to_an_object_of_OtherClass; // non-static data member static int i_m_belongs_to_OtherClass; // static data member }code> Only one copy is allocated for static data members, one copy per object for non-static data members

                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