I need help on java modifiers, pls.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mayfjf
    New Member
    • Jan 2007
    • 14

    I need help on java modifiers, pls.

    1. Hello, I wonder what are the differences between modifiers? Private, public, default, static...? I have reread and reread my java book, but I just can't understand. Thanks for any info...

    May
  • hirak1984
    Contributor
    • Jan 2007
    • 316

    #2
    look at this. or this

    Comment

    • mayfjf
      New Member
      • Jan 2007
      • 14

      #3
      Originally posted by hirak1984
      look at this. or this
      Thanks... It really helps!

      May

      Comment

      • hirak1984
        Contributor
        • Jan 2007
        • 316

        #4
        there is a request please dont post more than one thread with same question.It
        really makes people confused.
        Best of luck.

        Comment

        • mayfjf
          New Member
          • Jan 2007
          • 14

          #5
          Originally posted by hirak1984
          there is a request please dont post more than one thread with same question.It
          really makes people confused.
          Best of luck.
          Yeah, sorry for making anyone confused. I am really new to TSDN and I myself didn't know how did I post two threads with one subject. Maybe I have clicked the 'Submit reply ' button twice. Next time, i'd be very careful.

          May

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by mayfjf
            Yeah, sorry for making anyone confused. I am really new to TSDN and I myself didn't know how did I post two threads with one subject. Maybe I have clicked the 'Submit reply ' button twice. Next time, i'd be very careful.

            May
            Yeah you probably hit the button twice.

            Comment

            • dmjpro
              Top Contributor
              • Jan 2007
              • 2476

              #7
              1>Private is a modifier which can be used in class member
              with in the the class scope u can use those members...

              2>Public is a modifier whole packages can access those members

              3>Default is a modifier which can be accessed through out the same package only

              4>Static is modifier which is only accessed without using the class object reference.
              the static can be further public private or default.
              default means with out modifier specification it is default......

              thankkksksssss. ...........

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by dmjpro
                1>Private is a modifier which can be used in class member
                with in the the class scope u can use those members...

                2>Public is a modifier whole packages can access those members

                3>Default is a modifier which can be accessed through out the same package only

                4>Static is modifier which is only accessed without using the class object reference.
                the static can be further public private or default.
                default means with out modifier specification it is default......

                thankkksksssss. ...........
                Default is not a Java modifier, the modifier is protected.
                Static objects can be accessed using a class object reference.

                Comment

                • dmjpro
                  Top Contributor
                  • Jan 2007
                  • 2476

                  #9
                  in the same package the default access specifier and protected acts as same....
                  but in different package the protected can be accessed in subclasses but the default is not.

                  Comment

                  • mayfjf
                    New Member
                    • Jan 2007
                    • 14

                    #10
                    I have already tried some simple programs using static , and here is my sample codes, (these compile)...

                    Code:
                    //SAMPLE PROGRAM ON THE USE OF STATIC MODIFIER
                    
                    package java_samples;
                    
                    class samp1{
                    	
                    	public static void main(String args[]){
                    		System.out.println(1+2+"3");
                    		System.out.println("1"+ 2+1+2);
                    		class1.member1();
                    		class1.member2();
                    		System.out.println(class1.part1); // part1 is static, it can be   accessed even without creating an object.
                    	}
                    }
                    Code:
                    package java_samples;
                    
                    //import java.io.*;
                    
                     class class1{
                    	 static int part1 = 12;   // STATIC VARIABLE part1
                    	 static void member1(){	// STATIC METHOD member1; STATIC METHOD IS IMPORTANT  TO CALL part1
                    	 	
                    	 			
                    		System.out.println("HELLO, THIS IS MEMBER1!!");
                    		System.out.println(class1.part1); 
                    	}
                    	
                    	 static void member2(){
                    		
                    		class1.member1();
                    	}
                    }
                    But now, my question is: why can't I use the protected modifier on class1? class1 and samp1 belongs to the same package

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      Originally posted by mayfjf
                      I have already tried some simple programs using static , and here is my sample codes, (these compile)...

                      Code:
                      //SAMPLE PROGRAM ON THE USE OF STATIC MODIFIER
                       
                      package java_samples;
                       
                      class samp1{
                       
                      	public static void main(String args[]){
                      		System.out.println(1+2+"3");
                      		System.out.println("1"+ 2+1+2);
                      		class1.member1();
                      		class1.member2();
                      		System.out.println(class1.part1); // part1 is static, it can be accessed even without creating an object.
                      	}
                      }
                      Code:
                      package java_samples;
                       
                      //import java.io.*;
                       
                      class class1{
                      	 static int part1 = 12; // STATIC VARIABLE part1
                      	 static void member1(){	// STATIC METHOD member1; STATIC METHOD IS IMPORTANT TO CALL part1
                       
                       
                      		System.out.println("HELLO, THIS IS MEMBER1!!");
                      		System.out.println(class1.part1); 
                      	}
                       
                      	 static void member2(){
                       
                      		class1.member1();
                      	}
                      }
                      But now, my question is: why can't I use the protected modifier on class1? class1 and samp1 belongs to the same package
                      Post the code that you want to try the protected modifier with. I'm not really getting your question.

                      Comment

                      • mayfjf
                        New Member
                        • Jan 2007
                        • 14

                        #12
                        Originally posted by r035198x
                        Post the code that you want to try the protected modifier with. I'm not really getting your question.
                        I was just wondering before why cant I use the code:

                        Code:
                        protected class class1{
                        .
                        .
                        .
                        }
                        but I guess I have now the answer. Is it because protected is a method/field modifier??

                        Comment

                        Working...