0% found this document useful (0 votes)
17 views5 pages

PRACTICAL1

Uploaded by

Abhishek Goud
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views5 pages

PRACTICAL1

Uploaded by

Abhishek Goud
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

PRACTICAL 1

EXAMINE NAME AND SCOPE:


1.1 STATIC VARIBALE IN C
1.2 CALL RESOLUTION IN JAVA.

STATIC VARIABLE IN C:
In C programming, the concept of a static variable has a few different
meanings depending on where and how it is used. Let's explore the two
main uses of static variables in C:

### 1. Static Variables inside Functions

When a variable is declared as `static` inside a function, it retains its


value between function calls. Here's an example:

```c

#include <stdio.h>

void increment() {

static int count = 0; // static variable

count++;

printf("Count: %d\n", count);

int main() {

increment(); // Output: Count: 1

increment(); // Output: Count: 2

increment(); // Output: Count: 3

return 0;

In this example, the `count` variable is declared as `static int count = 0;`
inside the `increment` function. Unlike normal local variables, `count`
retains its value between successive calls to `increment()`. Therefore,
each time `increment()` is called, `count` is incremented and its updated
value is printed.
### 2. Static Variables outside Functions

When a variable is declared as `static` at the global scope (outside of any


function), it limits the scope of the variable to the file in which it is
declared. This means that the variable is not accessible from other files
that include the same header file. Here's an example:

**file1.c**

#include <stdio.h>

static int globalVar = 10; // static variable

void function1() {

printf("Global variable in file1.c: %d\n", globalVar);

**file2.c**

#include <stdio.h>

extern void function1(); // Declare function1 from file1.c

void function2() {

// Attempt to access globalVar from file1.c (won't compile)

// printf("Global variable in file2.c: %d\n", globalVar); // This line will


cause an error

In this example, `globalVar` is declared as `static int globalVar = 10;` in


`file1.c`. It is accessible only within `file1.c` and not visible to `file2.c`. If
you try to access `globalVar` in `file2.c` without an appropriate
declaration or definition, it will cause a compile-time error.

### Summary

- **Static variables inside functions**: Retain their value between function


calls.

- **Static variables outside functions (global scope)**: Limit the visibility of


the variable to the file where it is declared.

Understanding and appropriately using static variables in C can help in


managing program state and controlling variable scope effectively.
CALL RESOLUTION IN JAVA:
In Java, call resolution typically refers to how the Java compiler decides
which method or constructor to invoke when there are multiple options
that match the method signature in terms of name and parameter types.
The process of determining the most appropriate method or constructor to
call is known as **method overloading** and **method overriding**.

### Method Overloading

Method overloading occurs when multiple methods in the same class have
the same name but different parameters. The Java compiler determines
which method to call based on the number and types of arguments
passed to it. Consider this example:

```java

public class Example {

public void display(int num) {

System.out.println("Integer number: " + num);

public void display(String text) {

System.out.println("String text: " + text);

```

In the above code, the `display` method is overloaded with two versions:
one that takes an `int` parameter and another that takes a `String`
parameter. When you call `display(10)` or `display("Hello")`, Java
determines which version of the `display` method to execute based on
the argument type.

### Method Overriding

Method overriding occurs in the context of inheritance, where a subclass


provides a specific implementation of a method that is already defined in
its superclass. The method in the subclass must have the same signature
(method name and parameters) as the method in the superclass. Here's
an example:

```java

class Animal {

public void makeSound() {

System.out.println("Animal makes a sound");

class Dog extends Animal {

@Override

public void makeSound() {

System.out.println("Dog barks");

```

In this example, the `Dog` class overrides the `makeSound` method of


the `Animal` class. When you call `makeSound()` on a `Dog` object, it will
invoke the `makeSound` method from the `Dog` class rather than the one
from the `Animal` class.

### Call Resolution Rules

Java follows specific rules to resolve method calls:

1. **Exact Match**: The compiler looks for an exact match between the
method name and the argument types.

2. **Widening Primitive Conversion**: If an exact match isn't found, Java


tries to match by widening primitive types. For example, `int` can be
widened to `long`.

3. **Autoboxing**: Java can convert between primitive types and their


corresponding wrapper classes (e.g., `int` to `Integer`).

4. **Varargs**: If no exact match is found and the method accepts


variable arguments (`...`), Java can use varargs to match the method call.

### Conclusion
Understanding call resolution in Java involves knowing how the compiler
selects the correct method or constructor based on the method's name,
argument types, and the context of inheritance (for overriding). Method
overloading and overriding are fundamental concepts in Java
programming that allow you to write more flexible and reusable code by
providing multiple implementations of methods with the same name.

You might also like