Does Java require keyword "public" in class variables? [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I know that in JavaScript, you don't need the public keyword in the following code :
class myClass
{
public int myVariable;
// it is the same as :
int myVariable
}
do you need it in Java ? What is its purpose ?

Yes if you want something to be accessible everywhere.
Otherwise it is package-visibility, meaning only stuff in the same package (at some level) can access it.

You don't 'need' the 'public' keyword - if you don't specify the access level of a Class variable it will be set to package-private.
More details are here -
http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
public fields are a bad idea, however (A VERY bad idea in a multi-threaded application). It allows other classes to change the state of your class without any control, and could break invariants. The proper way is to control state-changing through public setter methods.

Related

isn't protected supposed to be accessible only by subclasses in Java? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 2 years ago.
Improve this question
I got really surprised that this code didn't get me any compile errors.
class A {
protected int a;
}
class B {
void b() {
A a = new A();
a.a = 4;
}
}
No, protected is visible to other classes in the same package.
The thing to remember is that access modifiers are meant to keep from bothering OTHERS with too much information. If you are working in the same package, then you’ve already got the information, use it if you want to.
In java protected variable is accessible within the package and outside the package but by use of inheritance. If any other class from outside the package, try to access these variables the compiler will throw an error at compilation time.
You can read it detail
https://javagoal.com/access-modifiers-in-java/#12

How do I access a field of an object from the object it contains [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
Okay let's say I have a Class Game. I instantiate Game as game. Now game contains an object, startupState. How do I change an int in game from startupState?
Sorry if i worded this poorly... code example:
public class game{
int state;
StartupState startupState;
public static void main(String[] args){
Game game = new Game();
}
public Game(){
state = 0;
startupState = new StartupState();
}
}
So with this code how would I change state from the startupState object?
You can't change a Game object from StartupState. The latter is an instance of a separate class, which may well exist without an instance of Game. You can, however, go the other way round, and change startupState from game (unless it's declared final). To change state, you will either need a setter (to access it from outside the current package) or you need to declare it public. Then, you can do
game.setState() // with setter
or
game.state = some_int // if it's public
I hope I interpreted this question correctly. So I assume that an instance of StartupState has a initialState member variable, maybe with a public int getInitialState() {...} instance method that returns it. In that case... theres no really straightforward way to change the state member variable of a Game instance from within the StartupState class (I am assuming thats what you want to do here). To do that, you must call getInitialState() from a instance method of the Game class and set this.state to the return value from the getInitialState() call.

Can you have a private attribute which type is an Interface? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
In our code we are supposed to have an Interface (let's call it InterfaceMap) that has the methods that let you put a cell in a Sheet (putCell(), getCell(), etc).
We have a class called OurSheetMatrix that implements that InterfaceMap. Can we have something on a different class Sheet like:
public class Sheet {
private InterfaceMap m = new OurSheetMatrix();
...
}
I think we might be able to do this not with Interfaces, but with abstract classes. But now, I'm not sure.
Yes. OurSheetMatrix is-a InterfaceMap. You could not however say new InterfaceMap(); because you can not instantiate an interface directly (It's probably best to think of them as promises [or more formally contracts]). Abstract classes are similar but distinct, it's the relationship that matters (and they can't be directly instantiated).
Yes you can do it :)
private InterfaceMap m = new OurSheetMatrix();
is OK. You can also pass references to your interface around in methods:
public void doSomething(InterfaceMap iamp) {
//Do something with an InterfaceMap.
//I don't know (or care) exactly what class it is,
//so long as it implements InterfaceMap
}
But if you have something more specific:
public void doSomethingElse(OurSheetMatrix matrix) {}
It cannot be called like this:
InterfaceMap imap = new OurSheetMatrix(); //this is OK
doSomethingElse(imap); //But this? NO! can't do this!
The above call to doSomethingElse would not compile because doSomethingElse wants a OurSheetMatrix. Although we know it's really a OurSheetMatrix the method does not know.
All OneSheetMatrix objects are also InterfaceMaps, but InterfaceMaps are not necessarily OneSheetMatrix objects, hence the call to doSomethingElse is not valid - there may be other classes which implement InterfaceMap.

How to create a class in java which instance cannot be created? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have to create a class. Instance of this class cannot be made. How can I achieve that?
Declare it abstract and add a private constructor.
Do you mean no instances can ever be made? or just one instance?
If no instances can ever be made, then make the class final with a private constructor. All methods then need to be static. A good example of this is java.lang.Math
If you need only a single instance and want no one else to make new instances, then consider using an enum as described in Effective Java 2nd ed:
public enum MyClass{
INSTANCE;
...methods
}
Code that uses this class then invokes methods like this: MyClass.INSTANCE.foo().
If you don't want to instantiate that class then I assume that you only want it to have some static methods. In that case you can easily do it with enum with no constants like
enum MyUtilities{
;//if you want you can place instances of that class here, or not place any
//your methods
public static void myMethod(){
System.out.println("hello");
}
}

Java: Calling non-static method with this. from static method [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I was wondering if I could use this. to call a non-static method from within a static method. I know I would need an object in general to reference to a non-static method from within a static method. Thanks
No. Within a static method, this has no meaning and will not compile. This is covered by §8.4.3.2 of the Java Language Specification, although it should be fairly intuitive — what would thisrefer to?
You can call non-static methods from static methods, just not via this. You have to have an instance on which to call them.
You cannot use "this" keyword in a static method.
The answer is NO. A static method is not associated with an instance of the class, so it cannot access a non static variable or method of the same class that has a meaning only if there is an instance of the class
Not using this, but if you really wanted to, if the class was named MyClass, you could do
new MyClass().someNonStaticMethod()
But if you're calling instance methods like this, they should probably be static anyway.

Categories

Resources