Solved

Java does not have global variables or functions

Posted on 2014-03-25
6
254 Views
Last Modified: 2014-04-24
Hi,

I was reading as below
All code in Java program is encapsulated within classes therefore Java does not have global variables or functions.

I have not understood what it means by
Java does not have global variables or functions.

Please advice.
Any links resources ideas highly appreciated. Thanks in advance
0
Comment
Question by:gudii9
  • 3
  • 3
6 Comments
 
LVL 35

Expert Comment

by:mccarl
ID: 39954698
Basically, it just means that you can define a function/variable that is available to your entire application, without it being defined inside a class.

For example, you can't define say a function called "doSomething()" that is available to be called from anywhere in your application using that syntax above. Your only option is to define that function inside a class, eg...
public class ABC {
    public static void doSomething() {
        // do something here
    }
}

Open in new window

And then you call that function by first qualifying it with the class name, ie. ABC.doSomething();

Or in other words, you can't have a function/variable that is NOT associated with a class (or an object of that class)
0
 
LVL 7

Author Comment

by:gudii9
ID: 39995089
For example, you can't define say a function called "doSomething()" that is available to be called from anywhere in your application using that syntax above.

which syntax above?

please advise. I only see code below which is clear.
0
 
LVL 35

Expert Comment

by:mccarl
ID: 39996969
I was referring to the "doSomething()" bit. So to expand, I was basically saying that you can't have a function called "doSomething()" and be able to call that code using a line like....
doSomething();

Open in new window

...from anywhere in your code.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 7

Author Comment

by:gudii9
ID: 40005167
:

I was referring to the "doSomething()" bit. So to expand, I was basically saying that you can't have a function called "doSomething()" and be able to call that code using a line like....

doSomething();
                                           


within same class we can do that right. please advise with complete example
0
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 40005595
within same class we can do that right
Yes, that is the ONLY way you can do it. But that was the point that I was making, if it is within the class it is therefore NOT global.

please advise with complete example
It's impossible to give you an example, since we are talking about something that you CAN'T do.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40006981
I do not know much about 'Java does not have global variables or functions' if they are c++ or c# terms 'global variables or functions' .

I was thinking ' global variables or functions' means 'Static variables nd static functions' as per my java knowledge. Please correct me if i am wrong.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
splitOdd10 challenge 5 74
Java passing arguments (type error) 2 47
print map entry 34 54
difference of if loops 23 38
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now