Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I want to know why this problem occurs.
public class Sorter {
public static char[] selectSort(String targetStr) {
private char[] charArray = new char[targetStr.length()];
The problem occurs when the variable charArray is made private and there is a message:
Modifier 'private' not allowed here.
Can anyone explain to me thoroughly why this happens so that I can better handle them in the future?
You can't have access modifiers to method local variables.
All the method local variables are visible only inside the method.
You cannot use access modifiers with fields declared within method scope (i.e. local fields).
So neither private nor public nor protected.
Here is a tutorial on Java variables that broaches the subject.
That char array(charArray ) is already local to that method., You cannot access that outside of that method anyway. So access modifier doesn't make sense there.
Scope of that variable is only till that method it cant be used outside that so there is no need to give access specifier to it.
Any variable created inside a method is local to that method only and it cant be accesed outside.
Scope of that variable char Array is only till that method it cant be used outside that so there is no need to give access specifier to it.
Related
This question already has answers here:
Why is there a problem with a non-static variable being read from main?
(3 answers)
Closed 6 years ago.
I seem to have a dilemma. Sparing the complicated details of my project, i'm attempting to do the following. (excluding and replacing some code like imports and try-catch for simplicity)
1 public class Client
2 {
3 private Registry reg1;
4 private GameSessionInterface sesh1;
5
6 public static void main(String[] args)
7 {
8 reg1 = LocateRegistry.getRegistry(serverIP, 4200);
9 sesh1 = (GameSessionInterface)reg1.lookup("Session1");
10 }
11 }
On lines 8 and 9 i get the errors "Cannot make a static reference to the non-static field reg1" and "Cannot make a static reference to the non-static field sesh1" respectively.
if i declare reg1 and sesh1 inside of main, i don't get this issue. But i need at the very minimum sesh1 to be global so i can make methods accessing it outside of main.
I'm not 100% sure how RMI variables work in the JVM so I'm unsure whether it's safe to declare these as static. Logic would follow that since i'm not going to be making more than one instance of Client in the same JVM, it shouldn't matter, but considering this is a reference to a remote object, I didn't know if this would have some unseen side effects. I've searched for a while and no one seems to address this. But at the same time I can't seem to find examples of code with these declared statically, which also begs the question why can't I compile it as is when similar code exists elsewhere with non-static declarations made globally.
I'm REALLY new to RMI in java so if any of you with more RMI experience could shed some light on how all this interacts and why I might be getting this error, I would greatly appreciate it.
Thanks in advance!
RMI variables are no different from any other variables. You could for example have declared both variables as local within main().
However if you're creating a Registry with LocateRegistry.createRegistry(), it is essential to store it into a static variable. Otherwise it can be garbage-collected and disappear.
the question why can't I compile it as is when similar code exists elsewhere with non-static declarations made globally
No it doesn't. Look again. The rules for static and instance variables are the same throughout Java. RMI does not and cannot change that.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
After reading global-variables-in-java, I found it's not suggested to declare global
variables. However, what's a better approach if I want to declare constant variables viewed by all methods in all classes. How to make those variables' scope global? How and why, thank you!
if I want to declare constant variables viewed by all methods in all classes
If those are really constants, then public static final fields are perfectly fine.
What kind of data is this? Does it need to externally configurable (at run time or build time)? Does it need to computed at application startup? Does that computation require external resources (such as files or networks) that may be unavailable or slow?
Well, the variables are indicating the state, in different drawing state(draw, erase...).
Not sure I understand what that means exactly, but maybe you want to define an enum?
I don't want to pass lots of parameter each time
That is a bad argument to use global variables.
Maybe you want to combine multiple arguments in a holder object?
Or have stateful objects with methods (as opposed to stateless functions that take many parameters).
But I still have to new a class containing these constants.
No. You can do
public final class MyConstants{
public static final String MY_OAUTH_KEY = "ABCDEFGH";
// maybe this should come from pom.xml
public static final String APP_VERSION = "0.0.1";
}
and then use it from anywhere in your code
System.out.format("You are running version %s%n", MyConstants.APP_VERSION);
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 8 years ago.
Improve this question
According to this post Global variables in Java it describes how to define a global variables in java by using static
public class Example {
public static int a;
public static int b;
}
But at same time in other post Why are there no global variables in Java? this question contradicts .
So my question is what exactly is global variable ?
Do java supports global variables ?if yes ,how?
if no ,why?
and how java global variables(if there are any) are different from c++ global variables?
I think that we can argue that there is no global keyword in java but your example can be treated like a global.
In most languages where you can define a global variable the problem is that they pollute the global namespace and name clashes can occur (like in php). In this regard there are no globals in java since there is no global namespace: variables are always in a class.
So the main thing is: there is no explicit globan in java and there is no globan namespace in java. This frees you from name clashes and accidental overwrites which is a good thing.
But there is nothing stopping you from creating a Global class with a lot of public static fields in it.
Please note that most guys (including me) would break both of your hands for doing so. :)
a gloabal variable is used by many functions without need to enter it in the input of these functions.
Expert advice to don't use them.
A global variable is a variable which can be accessed from anywhere (from any part of the program). Thus a public static variable in any Java class is global.
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am looking for a ans what I have found in facebook and became confused
"is it possible to declare a constructor inside a method"
Short answer: no.
Long answer: This comes from the Java Language Specification, §8.8:
A constructor is used in the creation of an object that is an instance of a class.
In all other respects, the constructor declaration looks just like a method declaration that has no result (§8.4.5).
Constructor declarations are not members. They are never inherited and therefore are not subject to hiding or overriding.
It's declared like this:
ConstructorDeclaration:
ConstructorModifiers(opt) ConstructorDeclarator
Throws(opt) ConstructorBody
ConstructorDeclarator:
TypeParameters(opt) SimpleTypeName ( FormalParameterList(opt) )
A ConstructorDeclarator can only live inside of the class body declaration:
ClassBody:
{ ClassBodyDeclarations(opt) }
ClassBodyDeclarations:
ClassBodyDeclaration
ClassBodyDeclarations ClassBodyDeclaration
ClassBodyDeclaration:
ClassMemberDeclaration
InstanceInitializer
StaticInitializer
ConstructorDeclaration <--
ClassMemberDeclaration:
FieldDeclaration
MethodDeclaration
ClassDeclaration
InterfaceDeclaration
;
A MethodDeclaration has no symbol to a ConstructorDeclaration, which is why you cannot declare a constructor inside of a method.
You cannot declare a constructor inside a method.
Constructors and methods are both components of objects. Your object has a constructor that gets called when you instantiate it. Once instantiated, your object then has properties to define it, and methods to do things with.
If you try to place a constructor into a declaration, you will get a compilation error.
This comes from several years of practice and study for the Sun certification tests.
No it is not possible.java does not support inner methods concept.Constructor is also like a method.Constructors can be declared inside a class only.
The constructor is sort of a method much like the main method. The constructor is used to initialize an object of the class and it has no return value.
Not at all possible.
A constructor must invoked while creating object for a class.
With out instance how you will call that method,which is building the constructor?.
Making any sense ?