By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,737 Members | 1,989 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,737 IT Pros & Developers. It's quick & easy.

I need help on java modifiers, pls.

mayfjf
P: 14
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
Jan 30 '07 #1
Share this Question
Share on Google+
11 Replies


hirak1984
100+
P: 316
look at this. or this
Jan 30 '07 #2

mayfjf
P: 14
look at this. or this
Thanks... It really helps!

May
Jan 30 '07 #3

hirak1984
100+
P: 316
there is a request please dont post more than one thread with same question.It
really makes people confused.
Best of luck.
Jan 30 '07 #4

mayfjf
P: 14
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
Feb 1 '07 #5

10K+
P: 13,264
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.
Feb 1 '07 #6

dmjpro
100+
P: 2,476
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............
Feb 3 '07 #7

10K+
P: 13,264
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.
Feb 3 '07 #8

dmjpro
100+
P: 2,476
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.
Feb 3 '07 #9

mayfjf
P: 14
I have already tried some simple programs using static , and here is my sample codes, (these compile)...

Expand|Select|Wrap|Line Numbers
  1. //SAMPLE PROGRAM ON THE USE OF STATIC MODIFIER
  2.  
  3. package java_samples;
  4.  
  5. class samp1{
  6.  
  7.     public static void main(String args[]){
  8.         System.out.println(1+2+"3");
  9.         System.out.println("1"+ 2+1+2);
  10.         class1.member1();
  11.         class1.member2();
  12.         System.out.println(class1.part1); // part1 is static, it can be   accessed even without creating an object.
  13.     }
  14. }
  15.  
  16.  
Expand|Select|Wrap|Line Numbers
  1. package java_samples;
  2.  
  3. //import java.io.*;
  4.  
  5.  class class1{
  6.      static int part1 = 12;   // STATIC VARIABLE part1
  7.      static void member1(){    // STATIC METHOD member1; STATIC METHOD IS IMPORTANT  TO CALL part1
  8.  
  9.  
  10.         System.out.println("HELLO, THIS IS MEMBER1!!");
  11.         System.out.println(class1.part1); 
  12.     }
  13.  
  14.      static void member2(){
  15.  
  16.         class1.member1();
  17.     }
  18. }
  19.  
  20.  
But now, my question is: why can't I use the protected modifier on class1? class1 and samp1 belongs to the same package
Feb 5 '07 #10

10K+
P: 13,264
I have already tried some simple programs using static , and here is my sample codes, (these compile)...

Expand|Select|Wrap|Line Numbers
  1. //SAMPLE PROGRAM ON THE USE OF STATIC MODIFIER
  2.  
  3. package java_samples;
  4.  
  5. class samp1{
  6.  
  7.     public static void main(String args[]){
  8.         System.out.println(1+2+"3");
  9.         System.out.println("1"+ 2+1+2);
  10.         class1.member1();
  11.         class1.member2();
  12.         System.out.println(class1.part1); // part1 is static, it can be accessed even without creating an object.
  13.     }
  14. }
  15.  
  16.  
Expand|Select|Wrap|Line Numbers
  1. package java_samples;
  2.  
  3. //import java.io.*;
  4.  
  5. class class1{
  6.      static int part1 = 12; // STATIC VARIABLE part1
  7.      static void member1(){    // STATIC METHOD member1; STATIC METHOD IS IMPORTANT TO CALL part1
  8.  
  9.  
  10.         System.out.println("HELLO, THIS IS MEMBER1!!");
  11.         System.out.println(class1.part1); 
  12.     }
  13.  
  14.      static void member2(){
  15.  
  16.         class1.member1();
  17.     }
  18. }
  19.  
  20.  
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.
Feb 5 '07 #11

mayfjf
P: 14
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:

Expand|Select|Wrap|Line Numbers
  1.  
  2. protected class class1{
  3. .
  4. .
  5. .
  6. }
  7.  
  8.  
but I guess I have now the answer. Is it because protected is a method/field modifier??
Feb 8 '07 #12

Post your reply

Sign in to post your reply or Sign up for a free account.