-5

What is the area enclosed in class and not enclosed in any method or block called in java?

What operations can we do here?

Is there a concept of global in java?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
sainath
  • 1
  • 1
  • Welcome to stack overflow. You might like to take a look at our [how to ask](https://stackoverflow.com/help/how-to-ask) page, for some help on how to format a question in a way likely to get you the most help. – Matt Sep 15 '18 at 06:47
  • That's where you declare class attributes (variables) – sid-m Sep 15 '18 at 06:47
  • 1
    What you describe is a *Field Declaration*. It is described in [JLS §8.3](https://docs.oracle.com/javase/specs/jls/se10/html/jls-8.html#jls-8.3). I am not sure what you mean by "operations" though... – Turing85 Sep 15 '18 at 06:50
  • 1
    1) It doesn't have a name. 2) The only operation you can do is to initialize a variable, as part of a variable declaration. 3) No. **However** - if you are trying to learn Java, then you would be advised to spend the time to read the Oracle Java Tutorial ... or a text book. – Stephen C Sep 15 '18 at 07:03
  • Can you give an example? What exactly are you not understanding about it that isn't found in the Java documentation? – OneCricketeer Sep 15 '18 at 07:04

3 Answers3

2

Java has no concept of "global". Classes have their own main scope which is the top level scope. You can only declare class member fields (class variables, or properties as its called in some languages like python, kotlin, c#, c++, etc) or methods, constructors, static blocks, or inner classes. The only way to access them is if they're visible and either declared static or accessed via an instance.

Alex Couch
  • 151
  • 4
  • There are "singletons", though. Not sure if you can call that "global" – OneCricketeer Sep 15 '18 at 07:06
  • Don't forget instance initialisation blocks. – Boris the Spider Sep 15 '18 at 07:09
  • @cricket_007 not sure what you mean. Singletons are classes with only one instance throughout the programs lifetime. – Alex Couch Sep 15 '18 at 07:47
  • @the Spider what do you mean "instance initialization blocks"? You mean constructors? EDIT: looked up "instantly initializer" and had no idea it existed. Its probably the same situation as labels where not many people know about them only cause they're not needed as often anymore right? – Alex Couch Sep 15 '18 at 07:47
  • Depends who you ask. They can be useful to share code between ctors. They are also seen in (the awful anti-pattern of) [double brace initialisation](https://stackoverflow.com/a/1958961/2071828) - although I hope no one actually uses that. – Boris the Spider Sep 15 '18 at 07:55
2

As you've found, you can declare methods. You can also declare/initialize fields, constructors, nested interfaces and classes; as well as static and instance initialization blocks.

The area is called the class body


JLS §8.1.6

A class body may contain declarations of members of the class, that is, fields (§8.3), methods (§8.4), classes (§8.5), and interfaces (§8.5).

A class body may also contain instance initializers (§8.6), static initializers (§8.7), and declarations of constructors (§8.8) for the class.

Community
  • 1
  • 1
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

There is no concept of, or any keyword for global, but a public, static variable in a class of your choice can be used effectively as a global variable ( accessible from anywhere inside your code).

As already mentioned in the comments a concept often used in java to perform "global" o unique operations, is a Singleton (design pattern) - a class you can have only one instance of (you can use it's constructor only once). That instance is stored in a static field of it's class, so in a way using a trick I've already mentioned (accessible by getter, rather than public).

I'm not aware of a precise name of the inside of a class in Java, but what you do there is mostly declare fields and sub classes ( and of course methods, but you've already mentioned that).

Arcimboldo
  • 31
  • 1
  • 4