Java public class modifier

R

rajesh.shiggaon

When we declare a class with public, all the methods and variable with
no specific modifiers, will be taken as public. Am i correct.

public class Test {

static void main(String args[]){
System.ot.println("Test message");
}

}




Will this work? I have checked it this is not working ? then wht is the
use of the public class modifier??

Thanks in advance
 
P

pole

When we declare a class with public, all the methods and variable with
no specific modifiers, will be taken as public. Am i correct.

Absolutely not. They are package-level visible.
 
A

Adam Maass

When we declare a class with public, all the methods and variable with
no specific modifiers, will be taken as public. Am i correct.

public class Test {

static void main(String args[]){
System.ot.println("Test message");
}

}

No. A public class is visible outside its package. The class-level
visibility modifier has no effect on the visiblity of the class' members.

-- Adam Maass
 
R

rajesh.shiggaon

Then if it is the case :-

According to the above message, it does not make any sense. I mean if
we dont declare a class public, then if we specify a public modifier to
a member of the class, we cant even access this member. Am i right?
Then why does not the java compiler prevent it from doing this.

One more point of view:-

class SomeClass {

public static void main(String args[]){
System.out.println("This class will compile and run");

}

}

We have not declared the class public, but how the java environment is
able to run this main method ??

Can anyone give a clear idea on this ??

Thanks in advance
 
R

rajesh.shiggaon

Then if it is the case :-

According to the above message, it does not make any sense. I mean if
we dont declare a class public, then if we specify a public modifier to
a member of the class, we cant even access this member. Am i right?
Then why does not the java compiler prevent it from doing this.

One more point of view:-

class SomeClass {

public static void main(String args[]){
System.out.println("This class will compile and run");

}

}

We have not declared the class public, but how the java environment is
able to run this main method ??

Can anyone give a clear idea on this ??

Thanks in advance


Adam said:
When we declare a class with public, all the methods and variable with
no specific modifiers, will be taken as public. Am i correct.

public class Test {

static void main(String args[]){
System.ot.println("Test message");
}

}

No. A public class is visible outside its package. The class-level
visibility modifier has no effect on the visiblity of the class' members.

-- Adam Maass
 
N

nerdjumbo

Hi Rajesh,

first of all, when we say "public static void main" this a special
combination of modifiers which the JVM looks for when we start the
compiled file..

1.)Public means.. it will be accessed from all over the packages
anywhere..
2.)Static means... u'll get the reference to it even before any of the
referecences to any class comes up......now here's is the catch since
this fuinction is available to the JVM.. even BEFORE anything else
loads.. hencefoth it can use its code to invoke other things..
 
R

rajesh.shiggaon

Hi :

I got your point, but my question is if we declare a method with the
same public static and try to acess it in different package, it will
not us to access it, since the class is not declared with the public
modifier. So how the JVM is able to call this function and why we are
not able to do that?

Thanks in advance
 
B

Bjorn Abelli

I got your point, but my question is if we declare a method with the
same public static and try to acess it in different package, it will
not us to access it, since the class is not declared with the public
modifier. So how the JVM is able to call this function and why we are
not able to do that?

The JVM is the runtime engine, and as such *all* classes, objects and their
members must be reachable for it.

The visibility modifiers restrict access between classes and objects.

// Bjorn A
 
M

Marcin Grunwald

Hi :

I got your point, but my question is if we declare a method with the
same public static and try to acess it in different package, it will
not us to access it, since the class is not declared with the public
modifier. So how the JVM is able to call this function and why we are
not able to do that?

Sometimes you want to forbid creating class instance but allow using it
outside the package.

For example:

package zz.zz.zz.pooling;

public interface Connection {
...
}

class FooConnection implements Connection {
...
}

public class ConnectionFactory {
...
public Connection getConnection() {
...
Connection con = new FooConnection();
...
return con;
}
...
}

- package "pooling" is responsible for maintaing connections and you can
easily use them, but you don't need to know how it works,
- you cann't cheat pooling and create connection on your own,
- you can easily replace FooConnection to Foo2Connection without changing
application code beacause you know nothing about FooConnection,
- ...
 
W

Wibble

The Java Launcher is written in C. The java native interface (JNI)
is has access to private members.

Then if it is the case :-

According to the above message, it does not make any sense. I mean if
we dont declare a class public, then if we specify a public modifier to
a member of the class, we cant even access this member. Am i right?
Then why does not the java compiler prevent it from doing this.

One more point of view:-

class SomeClass {

public static void main(String args[]){
System.out.println("This class will compile and run");

}

}

We have not declared the class public, but how the java environment is
able to run this main method ??

Can anyone give a clear idea on this ??

Thanks in advance


Adam said:
When we declare a class with public, all the methods and variable
with
no specific modifiers, will be taken as public. Am i correct.

public class Test {

static void main(String args[]){
System.ot.println("Test message");
}

}

No. A public class is visible outside its package. The class-level
visibility modifier has no effect on the visiblity of the class'
members.

-- Adam Maass
 
A

Adam Maass

Then if it is the case :-

According to the above message, it does not make any sense. I mean if
we dont declare a class public, then if we specify a public modifier to
a member of the class, we cant even access this member. Am i right?
Then why does not the java compiler prevent it from doing this.


In package foo:

public abstract class A {
public abtract void doSomething();
}


In package bar:

class B extends foo.A {
public void doSomething() {
System.out.println("I am a B");
}
}



Anywhere in your program, you can have a variable of type A (since this
class is public). The object (if any) it refers to might actually be of type
B.

Thus:

A a = ....
a.doSomething();

might print "I am a B" even if this fragment lives in a package other than
'bar', and class B isn't visible outside of 'bar'.
 
R

rajesh.shiggaon

Hi Guys:

Refering to the above messages, how do you instatiate type B, as this
class is not public. even if i declare a public B constructor it is not
allowing me to instantiate the type B and assign to A.

A a = ....
a.doSomething();

Connection con = new FooConnection();

So how this code is possible ? Am i missing out something ?

Thanks in advance

Rajesh S.
 
A

Arnaud Berger

Hi,

You may instanciate B from any class which shares the same package :

package bar;

public class C {


public C(){

B b=new B();

}


}

Default visibility (no modifier) being : package visibility

Regards,

Arnaud
 
A

Adam Maass

Hi Guys:

Refering to the above messages, how do you instatiate type B, as this
class is not public. even if i declare a public B constructor it is not
allowing me to instantiate the type B and assign to A.

A a = ....
a.doSomething();

Connection con = new FooConnection();

So how this code is possible ? Am i missing out something ?


Re-inserting my example:

In package foo:

public abstract class A {
public abtract void doSomething();
}


In package bar:

class B extends foo.A {
public void doSomething() {
System.out.println("I am a B");
}
}



I left this deliberately vague, but perhaps there's a class C in package
bar:

public class C {
public static foo.A create() { return new B(); }
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top