Question

Explain what an enumeration is and how you use one. Explain what static imports are and...

Explain what an enumeration is and how you use one.

Explain what static imports are and how you use them.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1) Enumeration

It is nothing but a user defined data type.To declare enumeration type we use the keyword "enum".Enumerated types consist of named values called enumerator or elements. enum is used to assign names to integral constants.

eg : In c# enum cards{club,diamond,heart,spade};

Usage:

eg in C program

Program Code:

#include<stdio.h>  

// It is similar to array index. Jan at index position 0, Feb at position 1 and so on.  
enum month{Jan, Feb, March, April,May, June, July,Aug,Sept,Oct,Nov,Dec};
  
int main()
{

//name is defined variable of type month
enum month name;
name = June;
printf("%d",name);
return 0;
}

Output:

5

2) Static Imports

It is a feature which was introduced in Java which allowed methods and fields to not specify class name when accessing them if they are static members or in other words it allows programmer to access static members directly without mentioning class name.

eg: Consider the case of using Math library in Java. By which we can use predefined methods such as pow(for calculating power) , sqrt(for square root ) and so on. By using static import we don't need to specify Math class when using those methods in the same scope.

Program Code:

//static import

import static java.lang.Math.*;

//creating a class

class Main{

public static void main(String[] args)

{

System.out.println(sqrt(4));

}

}

Output

2.0

Add a comment
Know the answer?
Add Answer to:
Explain what an enumeration is and how you use one. Explain what static imports are and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT