Solved

Java does not have global variables or functions

Posted on 2014-03-25
6
248 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

707 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