Solved

Java does not have global variables or functions

Posted on 2014-03-25
6
274 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 36

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 36

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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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 36

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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

632 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