java main() being static how it can have an access to non-static members of class? - static

In java, static methods can access only static variables then as main() is static and class contains static and non-static variables too then how come main() being a static can have access to non-static members of class?

Add an instance:
public static void main(String args[]){
ThisClass tc = new ThisClass();
tc.nonStaticVar//do whatever
}
This does not retrigger main because it isn't called.

Related

Is there any way to access static global variable defined in main.c and modify it in another file?

For example:
In main.c
static glob_var;
I want to modify the value of glob_var in another file say file1.c
Making an variable static makes its identifier inaccessible from other translation unit (what usually means other C-file). You can either
Make the variable non-static.
//main.c
int glob_var;
//file1.c
extern int glob_var;
Note that the declaration should be put to a header file.
Keep it static and add a helper function for access.
//main.c
static int glob_var;
void SetGlobVar(int val) {
glob_var = val;
}
//file1.c
void SetGlobVar(int);
void foo(void) {
SetGlobVar(42);
}
Note that the declaration of SetGlobVar() should be put to a header file.
The comments and other answer address the question of how to access and modify a static global". The following is offed as an alternative to using static for this purpose...
When needing to create a variable that is global and can be changed among several translation units I believe it is more idiomatic to use the externstorage class. This is typically done by:
Declaring the extern variable in a header file
Defining the extern variable one-time-only in a .c file that #includess the header file.
Access and modify the extern class variable from any translation unit that #includes the header file in which the extern is declared.
Example:
in some.h:
void modify_glob_var(int val);
...
extern int glob_var;//declare extern variable
in main.c
#include "some.h"
...
int glob_var = 0;// define extern variable
...
modify_glob_var(10);//access and modify extern variable
in some_other.c
include "some.h"
...
void modify_glob_var(int val)
{
glob_var = val;//value of extern glob_var is changed here
}

Cannot find function from another file in C

In my file gpio.c I have:
#include <modules/comm/module.h>
...
void testing(void)
{
u8_t buf[] = "lsjdflkdsjf";
cli_output(buf);
}
In gpio.h I have:
void testing(void);
In module.c I have:
#include <drivers/gpio/gpio.h>
...
static void cli_output(u8_t buf[])
{
printk("hi");
return 0;
}
static int fg_temp(int argc, char *argv[])
{
testing();
return 0;
}
In module.h I have:
static void cli_output(u8_t buf[]);
Does anyone know why I'm getting the following error?
<project dir>/drivers/gpio/gpio.c:108: undefined reference to `cli_output'
collect2: error: ld returned 1 exit status
My file system is such that /drivers and /modules are in the same main project directory if that is of any help. Thanks!
just remove static. static limits the scope of the function to it's own file.
So you get:
In module.c I have:
#include <drivers/gpio/gpio.h>
...
void cli_output(u8_t buf[])
{
printk("hi");
return 0;
}
static int fg_temp(int argc, char *argv[])
{
testing();
return 0;
}
In module.h I have:
void cli_output(u8_t buf[]);
In module.c, you define cli_output() as static function. Refer to tutorialspoint:
A static function in C is a function that has a scope that is limited
to its object file. This means that the static function is only
visible in its object file.
So there are 2 scenarios:
Use cli_output() as static function.
If you want to call cli_output() only in module.c, define it as static function, also add function prototype for cli_output() in module.c:
static void cli_output(u8_t buf[]);
Remember to remove the function prototype in module.h.
Use cli_output() as global function
Just remove the static keyword implemented for both definition and declaration of cli_output() in module.c and module.h.

File descriptors in C. Lugging them around vs static global

If I'm writing a library that uses a file descriptor for doing stuff, when should I return it from lib_init() for the higher layer code to use and pass to my lib_do_stuff() calls, and when can I leave it as a private "member" in my C library as a static global in .c file?
If I don't think the user of my library should have control or even access to the file descriptor, can I just leave it, much like in C++ it would just be private?
What are the downsides for doing it either way?
Expanding my suggestion with an example.
Your library needs two (at least) header files: One public that the users of your library includes, and one private that you include only in your library source files.
The public could be something like
#pragma once
// This is all that is needed to declare pointers to the internal structure
typedef struct internal_structure STRUCTURE;
// The public API of your library
STRUCTURE *lib_init(void);
void lib_cleanup(STRUCTURE *s);
...
Then you have the private header file
#pragma once
struct internal_structure
{
int fd;
// Other members as needed
...
};
// Possible function prototypes of private functions
Then in your library source files you include both the public and the private header files, and use STRUCTURE for the black-box structure:
#include <stdlib.h>
#include "public.h"
#include "private.h"
STRUCTURE *lib_init(void)
{
STRUCTURE *s = malloc(sizeof *s);
s->fd = open(...);
// Other initialization
...
return s;
}
void lib_cleanup(STRUCTURE *s)
{
// Other cleanup
...
close(s->fd);
free(s);
}
Then the users of your library includes only the public header file, and uses your well-defined API:
#include "public.h"
int main(void)
{
STRUCTURE *s = lib_init();
...
lib_cleanup(s);
return 0;
}
The public functions should all take STRUCTURE * as one of their arguments, typically their first argument, similar to the lib_cleanup function. The function can then use the structure and its members any way they want.

Scope of Function access in C

Suppose I have a function that I want to use internally such as private in Java or C++, but I am writing in C. In this case, I have a header file and a c file implementation and the c file has several functions that should not be available to the end user. Is it ok for me to not declare these functions in the header file in order to prevent access to them or is this bad practice / is there a better way?
That's pretty much what you would do in this case and add static to the declared functions. You would declare your public functions in the .h file and the private functions in the .c file.
Let's say you had a .h and .c file as such:
SomeFile.h
void doSomething1(void);
void doSomething2(void);
SomeFile.c
static void doSomething3(void);
static void doSomething4(void);
//These functions can be called by anyone
void doSomething1(void)
{
//function guts here
}
void doSomething2(void)
{
//function guts here
}
//These functions can only be called inside this file
static void doSomething3(void)
{
//function guts here
}
static void doSomething4(void)
{
//function guts here
}
Yes its ok to make some of the functions not available in the header file.
The other options are to separate the header files into 2. One for internal use and other for external user.
Apart from that also you can declare the functions static making them not visible outside the file scope
Declare these functions directly in the .c file as static functions and provide the definitions of the functions in the same file:
Ìn the .c file:
static void internal_func1(void);
static int interal_func2(int a);
static void internal_func1(void)
{
/* body of internal_func1 */
}
static int interal_func2(int a)
{
/* body of internal_func2 */
}
Adding the static keyword makes these functions invisible to other source files (the function identifier will have internal linkage).
Make such internal function static and keep it just in .c file.
See static functions:
If a function is to be called only from within the file in which it is declared, it is appropriate to declare it as a static function. When a function is declared static, the compiler will now compile to an object file in a way that prevents the function from being called from code in other files.
Or in the C standard:
6.2.2 Linkages of identifiers...
If the declaration of a file scope identifier for an object or a function contains the storage-class
specifier static, the identifier has internal linkage.1
1 A function declaration can contain the storage-class specifier static only if it is at file scope ....

C++/CLI, static constructor outside class declaration

How do I put body of static constructor of a managed class outside class declaration? This syntax seems to be compilable, but does it really mean static constructor, or just a static (=not visible outside translation unit) function?
ref class Foo {
static Foo();
}
static Foo::Foo() {}
Yes, that is the correct syntax to create a C++/CLI static constructor. You can know its not creating a static function since that is not a valid function declaration syntax. Functions must have the return type specified. Also, the compiler would complain that Foo() is not a member of class Foo if it weren't linking it to the constructor you declared in the class definition.
You can test the fairly easily:
using namespace System;
ref class Foo {
static Foo();
Foo();
}
static Foo::Foo() { Console.WriteLine("Static Constructor"); }
Foo::Foo() { Console.WriteLine("Constructor"); }
int main(array<System::String ^> ^args)
{
Foo ^f = gcnew Foo();
Console.WriteLine("Main");
}
This would output:
Static Constructor
Constructor
Main

Resources